application/json POST content not returned as binary data
Copy link to clipboard
Copied
Hi,
I have a legacy API that uses taffy and CF 2018, and I'm posting json with Content-Type: application/json to the API.
taffy checks for binary data it gets from getHTTPRequestData().content. The documentation for getHTTPRequestData says that binary data will be returned if the content type doesn't start with 'text' or if it is "application/x-www-form-urlencoded."
I have confirmed that the data are being posted with Content-Type: application/json, but getHTTPRequestData().content is returning a string. If I cfdump the string, I see the following which appears to contain a representation of the bytes:
{"Code":"\u16a0\u16c7\u16bb\u16eb\u16d2\u16e6\u16a6\u16eb\u16a0\u16b1\u16a9\u16a0\u16a2\u16b1\u16eb\u16a0","CodeID":"515","CodeDescription":"foo"}
The Code in the JSON was a test string with ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠ.
Would anyone have any ideas how data might be getting garbled before it gets to the taffy CF application.cfc (which extends the taffy.core.api cfc)?
<cffunction name="getRequestBody" access="private" output="false" hint="Gets request body data, which CF doesn't do automatically for some verbs">
<!--- Special thanks to Jason Dean (@JasonPDean) and Ray Camden (@ColdFusionJedi) who helped me figure out how to do this --->
<cfset var body = getHTTPRequestData().content />
<!--- on input with content-type "application/json" CF seems to expose it as binary data. Here we convert it back to plain text --->
<cfif isBinary(body)>
<cfset body = charsetEncode(body, "UTF-8") />
</cfif>
<cfreturn body />
Copy link to clipboard
Copied
If you use "application/x-www-form-urlencoded", I think the data will likely be passed as string. To ensure binary data will be passed, use "multipart-form-data".
Copy link to clipboard
Copied
'application/json' rather than 'application/x-www-form-urlencoded.' From
what I've read in the docs for getHTTPRequestData, the content should be
returned as binary data for a content type of 'application/json', and taffy
is written in a way that assumes that is the case. I've confirmed that
isBinary(getHTTPRequestData.content)
is false despite the content type being 'application/json'.
Copy link to clipboard
Copied
You're correct, application/json is text, not binary. I'm not sure why getHttpRequestData would treat it as binary. It used to be, back in the day, that "application/{whatever}" would actually be binary, but JSON is definitely not binary.
Dave Watts, Eidolon LLC

