Skip to content

Instantly share code, notes, and snippets.

@scott-ad-riley
Last active February 25, 2016 09:53
Show Gist options
  • Save scott-ad-riley/345df4430b0120cb86c2 to your computer and use it in GitHub Desktop.
Save scott-ad-riley/345df4430b0120cb86c2 to your computer and use it in GitHub Desktop.
Bug with CF serializeJSON and CF implicit type conversion that breaks things.
<!--- content with header application/json comes through as binary --->
<cfoutput>
#toString(getHttpRequestData().content)#
</cfoutput>
<cfscript>
endpointURL = "http://localhost:3000/endpoint.cfm"; //change this to your localhost/environment
public function bugRequest () {
var httpService = new http();
httpService.setMethod("post");
// post to the url of your endpoint.cfm
httpService.setURL(endpointURL);
var body = StructNew();
body["cid"] = "245672e4"; // note that this is stored as a string
scheduleBodyJSON = serializeJSON(body);
// removing the security prefix CF adds when it serializes JSON
scheduleBodyJSON = Replace(scheduleBodyJSON, "//", "");
httpService.addParam(type="header", name="Content-Type", value="application/json");
httpService.addParam(type="body", value=scheduleBodyJSON);
writeOutput(httpService.send().getPrefix().fileContent);
}
public function workingRequest () {
var httpService = new http();
httpService.setMethod("post");
// post to the url of your endpoint.cfm
httpService.setURL(endpointURL);
var body = StructNew();
body["cid"] = "72142984eh"; // note that this is stored as a string
scheduleBodyJSON = serializeJSON(body);
// removing the security prefix CF adds when it serializes JSON
scheduleBodyJSON = Replace(scheduleBodyJSON, "//", "");
httpService.addParam(type="header", name="Content-Type", value="application/json");
httpService.addParam(type="body", value=scheduleBodyJSON);
writeOutput(httpService.send().getPrefix().fileContent);
}
bugRequest();
workingRequest();
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment