Nexmo API& CFHttp POST

时间:2013-03-21 18:58:37

标签: coldfusion cfhttp nexmo

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

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

2 个答案:

答案 0 :(得分: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将解决这个问题。

答案 1 :(得分:1)

查看API文档,我觉得他们希望字段是表单值。以下摘自the documentation here

  

HTTP方法

     

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

     

POST的预期“内容类型”是“application / x-www-form-urlencoded”,但我们也支持“application / json”,“application / jsonrequest”,“application /将参数作为JSON对象发布时,x-javascript“,”text / json“,”text / javascript“,”text / x-javascript“,”text / x-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>

请注意,我将Accept标头设置为application/xml。根据文档,这也可能是application/json。根据您的需要更改该值。