https json cfhttp post

digitalwise
digitalwise used Ask the Experts™
on
We received this sample code in php

<?php

$json_request = <<<JSON
{
"api_key": "1231231",
"remote_user_id": "123456",
"firstname": "Teston",
"lastname": "Testor",
"email": "test@example.com",
"remote_product_id": "EXAMPLE_REMOTE_ID",
"amount_paid": "0.00",
"send_email":true
}
JSON;

$eox_api_url = "https://theurl";

$curl_cmd = "/usr/bin/curl -X POST -d '$json_request' $eox_api_url --header \"Content-Type:text/json\"";

$eox_api_response = `$curl_cmd`;

print $eox_api_response

?>

Open in new window


and I am trying to get it to work in coldfusion but getting a I/O Exception: peer not authenticated error message in dump so my guess is I have something wrong in my code

<cfset stFields =
{
"api_key": "123456789012345678901234567890",
"remote_user_id": "1231",
"firstname": "Test",
"lastname": "Test",
"email": "test@test.com",
"remote_product_id": "1231",
"amount_paid": "33",
"send_email":true
}>

<cfhttp url="https://theurl" method="post" result="httpResp" timeout="60">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="body" value="#serializeJSON(stFields)#">
</cfhttp>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
@digitalwise - I kept getting a CF error when I attempted to use the cfset variable as provided. Therefore, I added a single quote before and after your json data set. In the sample it was not necessary to serialize the json because it's already in a valid name/value pair format. I checked the json as found and using three different output options within my browser.

I used the following online json validator:
http://jsonlint.com/

The CF code I used for testing was basically what you provided:
<cfset stFields ='
{
"api_key": "123456789012345678901234567890",
"remote_user_id": "1231",
"firstname": "Test",
"lastname": "Test",
"email": "test@test.com",
"remote_product_id": "1231",
"amount_paid": "33",
"send_email":true
}'>

<!--- What does the json look like? --->
Test 1 <cfoutput> #serializeJSON(stFields)#</cfoutput><br><br><br>
Test 2 <cfoutput> #stFields#</cfoutput><br><br><br>
Test 3 <br><cfdump var="#stFields#">

Open in new window


As for your authentication error, did you double check the API Key along with the username & password?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial