I'm using ColdFusion 10's new REST API and I'm having trouble with UTF-8 characters. It seems that ColdFusion 10's new REST API completely mangles non-ASCII characters. Is there a way around this?
For example, let's say I have an endpoint like this:
<cfcomponent rest="true" restpath="/widgets" produces="application/json">
<cffunction name="echo" access="remote" httpmethod="PUT" returntype="string">
<cfreturn GetHttpRequestData().content />
</cffunction>
</cfcomponent>
This endpoint simply responds with exactly what was sent in the request.
Here is my request:
PUT http://www.mycompany.com/rest/v1.0/widgets HTTP/1.1
User-Agent: Fiddler
Host: www.mycompany.com
Content-Length: 15
こんにちは
Here is the response I get:
HTTP/1.1 200 OK
Content-Type: application/json
Server: Microsoft-IIS/7.5
Date: Mon, 26 Aug 2013 23:59:09 GMT
Content-Length: 37
�ん���
Note how the response is completely different from the request.
To be 100% certain that this is a problem with the request being mangled by ColdFusion and not a problem with the response being generated by ColdFusion, I enhanced the endpoint to also log the request body into a database, and sure enough, it was already mangled.
Any way around this?
I tested your CFC with this code:
<cfprocessingdirective pageencoding="utf-8">
<cfhttp method="put" url="http://localhost:8500/rest/restservices/widgets" result="httpResponse" charset="UTF-8">
<cfhttpparam type="body" value="こんにちは">
</cfhttp>
<cfdump var="#httpResponse#">
And got the same output as you. Modifying it to include a Content-Type header fixed it:
<cfprocessingdirective pageencoding="utf-8">
<cfhttp method="put" url="http://localhost:8500/rest/restservices/widgets" result="httpResponse" charset="UTF-8">
<cfhttpparam type="header" name="Content-Type" value="text/plain; charset=UTF-8">
<cfhttpparam type="body" value="こんにちは">
</cfhttp>
<cfdump var="#httpResponse#">
So I guess this confirms what AdamT (and Peter) suggested: you need to specify the Content-Type.
I can't guarantee that adding a Content-Type header and specifying a charset of UTF-8 would fix your problem; but what I can tell you is that Taffy assumes UTF-8 and it doesn't have this problem:
Thumbnail: Click for full size http://note.io/1dLswMN
Here's an excerpt of Taffy's code that gets the request body:
<cffunction name="getRequestBody" access="private" output="false" hint="Gets request body data, which CF doesn't do automatically for some verbs">
<cfset var body = getHTTPRequestData().content />
<cfif isBinary(body)>
<cfset body = charsetEncode(body, "UTF-8") />
</cfif>
<cfreturn body />
</cffunction>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With