2013-03-21 71 views
2

我正在尝试使用CFHttp发布到Nexmo API。Nexmo API&CFHttp POST

API documentation

<cfhttp url="https://rest.nexmo.com/number/buy/" method="post"> 
    <cfhttpparam name="api_key" value="#api.key#" type="url"> 
    <cfhttpparam name="api_secret" value="#api.secret#" type="url"> 
    <cfhttpparam name="country" value="US" type="url"> 
    <cfhttpparam name="msisdn" value="11234567890" type="url"> 
</cfhttp> 

我得到这个运行时状态420(参数错误)。

我在做什么错?

这里有一个例子在PHP中:API

+0

你的代码对我来说是正确的。它是否认识到你所要求的isdn是无效的? – BKK 2013-03-21 19:22:34

+0

@BenKoshy我已经检查过所有传递的内容都是有效的。在发布之前,我只是更改了号码。 – Dre 2013-03-21 19:45:59

回答

1

看看API文档,我觉得他们期望字段是表单值。下面是从the documentation here的摘录:

HTTP方法

所有请求通过HTTP POST提交或使用UTF-8编码和URL编码值GET方法。

POST的预期“Content-Type”是“application/x-www-form-urlencoded”,但是我们也支持“application/json”,“application/jsonrequest”,“application/x-javascript” ,“text/json”,“text/javascript”,“text/x-javascript”,“text/x-json”作为JSON对象发布参数时。

所以试着改变你的代码如下:

<cfhttp url="https://rest.nexmo.com/number/buy/" method="post" charset="utf-8"> 
    <cfhttpparam name="Content-Type" value="application/x-www-form-urlencoded" type="header"> 
    <cfhttpparam name="Accept" value="application/xml" type="header"> 
    <cfhttpparam name="api_key" value="#api.key#" type="formField"> 
    <cfhttpparam name="api_secret" value="#api.secret#" type="formField"> 
    <cfhttpparam name="country" value="US" type="formField"> 
    <cfhttpparam name="msisdn" value="11234567890" type="formField"> 
</cfhttp> 

请注意,我有接受头设置为application/xml。根据文件,这也可能是application/json。根据你想要的改变这个值。

+0

同样的错误使用'type =“formfield”' – Dre 2013-03-21 19:39:15

+0

@Dre也许你还需要包含字符编码。我更新了我的代码示例。 – 2013-03-21 19:41:45

+0

仍然没有运气。我也试过'encoded =“yes”',它不起作用。 – Dre 2013-03-21 19:47:16

1

尝试改用formfield

<cfhttp url="https://rest.nexmo.com/number/buy/" method="post"> 
    <cfhttpparam name="api_key" value="#api.key#" type="FormField"> 
    <cfhttpparam name="api_secret" value="#api.secret#" type="FormField"> 
    <cfhttpparam name="country" value="US" type="FormField"> 
    <cfhttpparam name="msisdn" value="11234567890" type="FormField"> 
</cfhttp> 

此文档正在寻找一个POST和您的发送组合后/获得。根据你发送的内容,你不发送变量。 FormField将解决这个问题。