ColdFusion 10 REST API:如何在有效负载主体中解析 JSON [英] ColdFusion 10 REST API: How to parse JSON in body of payload

查看:17
本文介绍了ColdFusion 10 REST API:如何在有效负载主体中解析 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ColdFusion 10 的新内置 RESTful Web 服务功能.发布数据时,我想在请求正文中将有效负载作为 JSON 发送.例如:

I'm using ColdFusion 10's new build-in RESTful web services feature. When posting data, I'd like to send the payload as JSON in the body of the request. For example:

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

一旦这些数据通过 API 发布,我应该如何解析和反序列化服务器上​​的 JSON 数据?ColdFusion REST 服务是否具有执行此操作的内置方法?似乎通过将 cfargument 上的 restargsource 属性设置为form"来反序列化form"类型(即 content-type application/x-www-form-urlencoded),但我找不到任何示例关于如何本机反序列化 JSON 数据.我希望有类似 restargsource="json" 的东西,但那不存在.推荐的方法是什么?

Once this data is posted through the API, how should I parse and deserialize the JSON data on the server? Does ColdFusion REST service have a built-in way to do this? It seems that there is native support to deserialize "form" type (i.e. content-type application/x-www-form-urlencoded) by setting the restargsource attribute on cfargument to "form", but I'm not able to find any examples on how to deserialize JSON data natively. I was hoping for something like restargsource="json", but that doesn't exist. What is the recommended way to do this?

推荐答案

经过大量研究,ColdFusion 10 的 REST API 请求处理程序似乎没有原生方式为我们自动解析 JSON 请求.我们需要手动执行以下操作:

After a lot of research, it doesn't look like there's a native way for ColdFusion 10's REST API request handler to parse JSON requests automatically for us. We need to do this manually as follows:

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

这篇关于ColdFusion 10 REST API:如何在有效负载主体中解析 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆