json - 如何使用 Groovy 代码删除响应正文中 Gerrit 的 REST API 的魔术前缀?

标签 json rest groovy gerrit

我的代码:

@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/

相关文章:

java - 防止 Restful 中的 xml 转义 (JAXRS)

java - 尝试在 XPage 中使用 customRestService 和 serviceBean 作为 dojo 树时陷入困境

java - Spring Tool Suite (STS) 3.5.0 - org.codehaus.groovy.eclipse 在求解器中未知

maven - 使用 gradle 执行 Maven 部署文件

json - 合并从wordpress导出的两个Json文件

json - 在编译时不知道 key 的 circe 中解码 JSON 值

javascript - 使用 .getJSON 从 php 文件检索 JSON 数据

javascript - JSON parseError - 将 json 获取为字符串

java - REST是API,还是: REST vs Java Interface?

jenkins - 如何禁用 "build now"选项?