I'm sure that I'm completely botching this up but I got this far with the help of fellow Stack Overflow users, so thanks thus far.
I need to POST JSON data to a remote API. Obviously I can't use jQuery due to SOP issues, and the remote API does not support JSONP.
I also don't want to have to use any type of proxy as to get around the SOP limitations.
Per the API docs (http://myemma.com/api-docs/), this is the formatting of the data they expect (request and response data is transferred as JSON):
POST https://api.e2ma.net//123/members/add
{
"fields": {
"first_name": "myFirstName"
},
"email": "[email protected]"
}
And this is what I've built thus far but continue to receive "unable to parse JSON" errors from the remote API:
<cfset fields[name_first]="#SerializeJSON( "myFirstName" )#" />
<cfset form.email="#SerializeJSON( "[email protected]" )#" />
<cfhttp
url="https://api.e2ma.net/123/members/add"
method="POST"
username="username"
password="pssword"
useragent="#CGI.http_user_agent#"
result="objGet">
<!--- add email --->
<cfhttpparam
type="formfield"
name="email"
value='#form.email#'
/>
<!--- add field: name_first --->
<cfhttpparam
type="formfield"
name="fields"
value='#fields[name_first]#'
/>
</cfhttp>
<cfoutput>#objGet.FileContent#</cfoutput>
Again, I'm surely mangling the structure of my data somehow, but I'm not sure what I'm doing wrong, particularly regarding properly setting the "fields": { "first_name": "myFirstName" } structure/array.
Given the way you are submitting the data you should not have to serialize the strings, just
From your comment, that didn't work for you. Unfortunately their docs are confusing IMO as to how the data should be sent. They say it should be a post but then show only a json object. Maybe that is useful if using from JS but confusing otherwise.
To narrow down where the problem is occurring try submitting the information statically, for example take their example code and paste into the values of the fields. You should first try to get a static attempt going before a dynamic version. It may even be that the CF json serialization is tripping things up due to case-sensitivity or other issues.