Coldfusion CFHTTP UTF-8 Problem with Non-ASCII characters when receiving application/json responses

It cost me quite some time to figure this out.

Consider this scenario: You are using CFHTTP to request some JSON data from an external API. The data is sent UTF-8 encoded and everything is fine as long as the API uses Content-Type: text/html. Now if the Content-Type changes to application/json everything will break and the characters get messed up. Now what if you can’t tell the external API to use text/html as Content-Type? Besides application/json is perfectly fine and UTF-8 by design.

Here’s the trick:

When you access cfhttp.filecontent use cfhttp.filecontent.toString("UTF-8") instead and the response will be correct again.

If you've read this far you might as well follow me on Twitter here.


Comments