用于Restlet客户端的HTTP 400的JSON表示形式 [英] JSON Representation of a HTTP 400 for Restlet Client
问题描述
当需要400时,我正在努力访问Restlet客户端响应的JSON表示形式.
I am struggling to access a JSON Representation of a Restlet Client Response when a 400 is expected.
任何400响应均作为异常返回.我无法从异常获取JSON.有什么想法吗?
Any 400 response returns as an exception. I cannot get the JSON from an exception. Any ideas?
代码:
public def run(Object testCaseData) {
ClientResource clientResource = RestAPIUtils.setClientResource(GroovyUtils.getDataValue(testCaseData, 'command').toString(), partnerAPICredential)
def JSONData = JSONUtils.createJSONObject(testCaseData)
Representation representation = RestAPIUtils.getRepresentation(JSONData)
try {
clientResource.post(representation).getText()
}
catch (Exception ex) {
println ex
}
}
例如,如果我通过SoapUI运行相同的请求,则JSON表示将显示以下内容:
For example, if I run the same request via SoapUI, I see the following for the JSON representation:
{"errors": [{
"code": "VALUE_INVALID",
"field": "type",
"description": "Field value is invalid."
}]}
我无法从当前代码中获得该信息.
I can't get this from my current code.
推荐答案
环顾四周后,我注意到clientResource对象确实包含JSON表示形式.下面是更新的代码:
After looking around, I noted that the clientResource object does contain the JSON Representation. Below is the updated code:
public def run(Object testCaseData) {
ClientResource clientResource = RestAPIUtils.setClientResource(GroovyUtils.getDataValue(testCaseData, 'command').toString(), partnerAPICredential)
def JSONData = JSONUtils.createJSONObject(testCaseData)
Representation representation = RestAPIUtils.getRepresentation(JSONData)
try {
clientResource.post(representation).getText()
}
catch (Exception ex) {
clientResource.response.entityAsText
}
}
在这种情况下,我需要返回'clientResource.response.entityAsText'对于有一天可能遇到的所有Java用户,您需要返回:'clientResource.getResponse().getEntityAsText()'
In this case, I need to return 'clientResource.response.entityAsText' For all the java users who may come across this someday you need to return: 'clientResource.getResponse().getEntityAsText()'
这篇关于用于Restlet客户端的HTTP 400的JSON表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!