2013-03-21 74 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將解決這個問題。