我正在尝试使用 CFHttp 来 POST 到 Nexmo API。
<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
最佳答案
查看 API 文档,在我看来,他们期望字段是表单值。以下是 the documentation here 的摘录:
HTTP Methods
All requests are submitted through the HTTP POST or GET method using UTF-8 encoding and URL encoded values.
Expected "Content-Type" for POST is "application/x-www-form-urlencoded", however we also support "application/json", "application/jsonrequest", "application/x-javascript", "text/json", "text/javascript", "text/x-javascript", "text/x-json" when posting parameters as a JSON object.
因此请尝试将代码更改为以下内容:
<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 header 设置为 application/xml
。根据文档,这也可能是 application/json
。根据您的需要更改该值。
关于coldfusion - Nexmo API 和 CFHttp POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15555921/