Joh*_*ika 4 rest coldfusion coldfusion-10
我正在使用ColdFusion 10的新内置RESTful Web服务功能.发布数据时,我想将有效负载作为JSON发送到请求正文中.例如:
PUT https://mycompany.com/rest/v1.0/widget/261469 HTTP/1.1
Host: mycompany.com
Connection: keep-alive
Content-Length: 13
Content-Type: application/json
{"foo":"bar"}
Run Code Online (Sandbox Code Playgroud)
一旦通过API发布此数据,我应该如何解析和反序列化服务器上的JSON数据?ColdFusion REST服务是否有内置的方法来执行此操作?通过将cfargument上的restargsource属性设置为"form"似乎有反向序列化"表单"类型(即内容类型application/x-www-form-urlencoded)的原生支持,但我无法找到任何示例关于如何本地反序列化JSON数据.我希望像restargsource ="json"这样的东西,但那不存在.建议的方法是什么?
经过大量研究后,看起来ColdFusion 10的REST API请求处理程序无法为我们自动解析JSON请求.我们需要手动执行以下操作:
<cfset var json = ToString(GetHttpRequestData().content) />
<cfif !IsJSON(json)>
<cfthrow errorCode="400" type="ArgumentException" message="#"Invalid JSON string: " & json#" />
</cfif>
<cfset var jsonObject = DeserializeJSON(json) />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1842 次 |
| 最近记录: |