parsing - 获取响应时出现 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)
}
}```
解决方案
由于您使用的是http.request(POST, ContentType.JSON) {...}
,HTTPBuilder 将假定请求和响应都是 json。换句话说,它会尝试将响应解析为 json,即使它是 html,即使响应content-type
标头表明它是 html。
我猜您的发布请求会导致错误,而该错误又会在响应中作为带有一些错误文本的 html 页面返回。您尝试将此错误解析为 json 并最终导致您的问题失败。
您可以尝试ContentType.ANY
查看响应是什么。之后,可能值得阅读HTTPBuilder 的 javadocs,其中包含有关请求和响应的内容类型处理的大量详细信息。
推荐阅读
- flutter - Flutter Bloc A 在 Bloc B 中添加流,但此流不会通过 StreamBuilder 在 UI 中呈现
- awk - 替换包含特殊字符 & 的句子
- flutter - 使用 noSuchMethod 在 Dart 中做代理?
- pytorch - 使用预训练的 resnet 实现 Squeeze 和 Excitation 块
- javascript - 将条目数据发布到 html 表行
- android - Android:JobScheduler 有时会重用同一个作业实例?
- node.js - 为什么我的 Dockerized LoopBack4 应用程序运行失败并收到消息“ConnectionError: Failed to lookup instance on”?
- c# - 无法上传 .snupkg 文件:校验和与 dll 和相应的 pdb 不匹配
- javascript - 节点面向对象。链接类
- mysql - 生成表查询中的行数| 微软访问