ColdFusion 10 REST API UTF-8字符

时间:2013-08-27 00:04:58

标签: rest utf-8 coldfusion coldfusion-10

我正在使用ColdFusion 10的新REST API,而我遇到UTF-8字符问题。似乎ColdFusion 10的新REST API完全破坏了非ASCII字符。有办法解决这个问题吗?

例如,假设我有一个这样的端点:

<cfcomponent rest="true" restpath="/widgets" produces="application/json">

    <cffunction name="echo" access="remote" httpmethod="PUT" returntype="string">       
        <cfreturn GetHttpRequestData().content />
    </cffunction>

</cfcomponent>

此端点只是简单地响应请求中发送的内容。

这是我的要求:

PUT http://www.mycompany.com/rest/v1.0/widgets HTTP/1.1
User-Agent: Fiddler
Host: www.mycompany.com
Content-Length: 15

こんにちは

以下是我得到的回复:

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

�ん���

请注意响应与请求完全不同。

要100%确定这是ColdFusion请求被破坏的问题而不是ColdFusion生成响应的问题,我增强了端点以将请求主体记录到数据库中,果然,它已经被破坏了。

有什么方法吗?

4 个答案:

答案 0 :(得分:3)

我使用以下代码测试了您的CFC:

<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#">

并获得与您相同的输出。修改它以包含一个内容类型标题修复它:

<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#">

所以我猜这证实了AdamT(和Peter)的建议:你需要指定Content-Type

答案 1 :(得分:2)

我无法保证添加Content-Type标头并指定UTF-8的字符集可以解决您的问题;但我可以告诉你的是Taffy 假定 UTF-8并且doesn't have this problem

Thumbnail: Click for full size http://note.io/1dLswMN

以下是获得请求正文的Taffy代码的摘录:

<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>

答案 2 :(得分:1)

还有另一种方法可以将它与ColdFusion生成自定义休息响应的能力结合起来。此示例显示如何将响应强制为UTF-8。

<cffunction name="echo" access="remote" httpmethod="PUT" returntype="void">       
    <cfset response = structNew() >
    <cfset response.status = "200">
    <cfset response.statusText = "OK">
    <cfset response.headers["charset"] = "UTF-8">
    <cfset response.content = GetHttpRequestData().content>
    <cfset restsetResponse(response)>
</cffunction>

到目前为止,这是我发现的非ASCII字符集的唯一快乐解决方法。使用隐式REST的东西很有效,直到你需要UTF-8。我发现甚至设置请求字符集都会被击中或错过,特别是如果你从数据库中取出UTF-8内容。此覆盖部分记录在此处:http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WSe61e35da8d318518-5719eac51353e6bb244-7fec.html

答案 3 :(得分:0)

对于那些仍然无法在响应中返回UTF-8字符的人,看起来将端点更改为返回字符串而不是struct来解决问题。

示例(返回struct):

<cffunction name="foo" access="remote" httpmethod="GET" produces="application/json" restpath="foo" returntype="struct">

    <cfreturn {
        hello = "こんにちは"
    } />

</cffunction>

响应:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Fri, 06 Dec 2013 17:58:25 GMT
Content-Length: 17

{"HELLO":"?????"}

请注意,返回struct时,ColdFusion的内部序列化程序会破坏UTF-8字符。

另一方面,这是一个示例,其中方法返回字符串,我自己进行JSON序列化:

<cffunction name="foo" access="remote" httpmethod="GET" produces="application/json" restpath="foo" returntype="string">

    <cfreturn SerializeJSON({
        hello = "こんにちは"
    }) />

</cffunction>

响应:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Fri, 06 Dec 2013 17:59:12 GMT
Content-Length: 27

{"HELLO":"こんにちは"}

这很奇怪,但似乎有效。