我的代码:
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')
@Grab('oauth.signpost:signpost-core:1.2.1.2')
@Grab('oauth.signpost:signpost-commonshttp4:1.2.1.2')
import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*
def gerrit = new RESTClient('http://localhost:8080/gerrit')
gerrit.auth.basic "gerrit", "password123"
gerrit.get( path: 'changes/1234/reviewers' )
根据 https://gerrit-review.googlesource.com/Documentation/rest-api.html#authentication
“为了防止跨站脚本包含 (XSSI) 攻击,JSON 响应正文以一个神奇的前缀行开头,在将响应正文的其余部分提供给 JSON 解析之前必须将其删除r :“
)]}'
[ ... valid JSON ... ]
我的错误:
WARNING: Error parsing 'application/json; charset=UTF-8' response
groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
The current character read is ')' with an int value of 41
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0
)]}'
最佳答案
抱歉,我对 Groovy 不太熟悉,但是除非您想修改源代码并推出自己的版本,否则无法从 Gerrit 中删除此前缀。
解决方案是使用不同的 Groovy API 来获取原始数据,去掉前 5 个字符,然后将其输入到 Groovy JSON 解析器中。再次抱歉,我无法帮助您了解 Groovy API 可能是一个选项。
正如文档中提到的,这些字符有时很烦人,但这是 Gerrit 防止任何 XSS 黑客攻击的重要安全功能。
关于json - 如何使用 Groovy 代码删除响应正文中 Gerrit 的 REST API 的魔术前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618736/