首页 > 解决方案 > 获取响应时出现 Groovy 错误:groovyx.net.http.HTTPBuilder handleResponse WARNING: Error parsing 'text/html' response groovy.json.JsonException

问题描述

当前读取的字符为 '<',int 值为 60 无法确定当前字符,它不是字符串、数字、数组或对象 行号 1 索引号 0

*body for the POST method*
```
def jsonBody = [
"grant_type": "refresh_token",
"client_id": "XXXXXXXXXXXXXXXX",
"refresh_token": "XXXXXXXXXXXXXXXXXXXXXXX"
]
def http = new HTTPBuilder('https://account.uipath.com/oauth/token') 

http.request(POST, ContentType.JSON) {

//requestContentType = ContentType.JSON

print("inside the request body")
request.addHeader("ContentType", "application/json")

request.addHeader("Host","")

request.addHeader("X-UIPATH-TenantName","XXXXXXXXXXXXX")
print(jsonBody)
body = jsonBody
print("After json body")
response.success = { resp, JSON ->
JSON ?: [:]
print("Success ")
print("Success "+resp)
}

response.failure = { resp, JSON ->
JSON ?: [:]
print("Fail "
print("Fail "+JSON)
}
}```

标签: parsinggroovyhttpbuilderuipath-api

解决方案


由于您使用的是http.request(POST, ContentType.JSON) {...},HTTPBuilder 将假定请求和响应都是 json。换句话说,它会尝试将响应解析为 json,即使它是 html,即使响应content-type标头表明它是 html。

我猜您的发布请求会导致错误,而该错误又会在响应中作为带有一些错误文本的 html 页面返回。您尝试将此错误解析为 json 并最终导致您的问题失败。

您可以尝试ContentType.ANY查看响应是什么。之后,可能值得阅读HTTPBuilder 的 javadocs,其中包含有关请求和响应的内容类型处理的大量详细信息。


推荐阅读