coldfusioncfhttpvonage

Nexmo API & CFHttp POST


I am trying to use CFHttp to POST to the 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>

I get a status 420 (wrong parameters) when running this.

What am I doing wrong?

Here is an example in PHP: API


Solution

  • Looking at the API documentation it appears to me that they are expecting the fields to be form values. Here is an excerpt from 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.

    So try changing your code to the following:

    <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>
    

    Note that I have the Accept header set to application/xml. According to the docs this could also be application/json. Change that value depending on what you want.