【问题标题】:Groovy error in getting response: groovyx.net.http.HTTPBuilder handleResponse WARNING: Error parsing 'text/html' response groovy.json.JsonException获取响应时出现 Groovy 错误:groovyx.net.http.HTTPBuilder handleResponse WARNING: Error parsing 'text/html' response groovy.json.JsonException
【发布时间】:2021-01-20 08:13:14
【问题描述】:

当前读取的字符是 '

*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)
}
}```

【问题讨论】:

    标签: parsing groovy httpbuilder uipath-api


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 2019-09-30
      相关资源
      最近更新 更多