Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何删除Gerrit';s REST API';用Groovy代码在响应体中添加了什么魔法前缀?_Json_Rest_Groovy_Gerrit - Fatal编程技术网

Json 如何删除Gerrit';s REST API';用Groovy代码在响应体中添加了什么魔法前缀?

Json 如何删除Gerrit';s REST API';用Groovy代码在响应体中添加了什么魔法前缀?,json,rest,groovy,gerrit,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

我的代码:

@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' ) 
依照

“为了防止跨站点脚本包含(XSSI)攻击,JSON响应体以一个神奇的前缀行开始,在将响应体的其余部分提供给JSON解析之前,必须去掉该前缀行:r:”

我的错误:

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黑客攻击。

我找到了答案

@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.Method.*
import static groovyx.net.http.ContentType.*
import groovy.json.JsonSlurper

public class GerritRestClient {
    def gerrit

    GerritRestClient(config) {
        def username = config.username
        def password = config.password
        this.gerrit = new RESTClient(config.endpoint)
        this.gerrit.auth.basic config.username, config.password
    }

  def sendRequest(path) {
    this.gerrit.request(GET,TEXT) { req ->
      uri.path = path // overrides any path in the default URL
      response.success = { resp, json ->
        assert resp.status == 200
        def fixedJson = json.text.replaceAll('\\)]}\'', '').trim() // remove magic prefix
        def jsonSlurper = new JsonSlurper()
        return jsonSlurper.parseText(fixedJson) // parse the fixed json body and return
      }
      // called only for a 404 (not found) status code:
      response.failure = { resp ->
        println "My response handler got response: ${resp.statusLine}"
        return null
      }
    }
  }

  def getChangeReviewers(changeNumber) {
    def requestPath = "changes/${changeNumber}/reviewers"
    return sendRequest(requestPath)
  }

}

谢谢我使用了相同的Groovy API Rest客户机,只是需要做不同的事情。
@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.Method.*
import static groovyx.net.http.ContentType.*
import groovy.json.JsonSlurper

public class GerritRestClient {
    def gerrit

    GerritRestClient(config) {
        def username = config.username
        def password = config.password
        this.gerrit = new RESTClient(config.endpoint)
        this.gerrit.auth.basic config.username, config.password
    }

  def sendRequest(path) {
    this.gerrit.request(GET,TEXT) { req ->
      uri.path = path // overrides any path in the default URL
      response.success = { resp, json ->
        assert resp.status == 200
        def fixedJson = json.text.replaceAll('\\)]}\'', '').trim() // remove magic prefix
        def jsonSlurper = new JsonSlurper()
        return jsonSlurper.parseText(fixedJson) // parse the fixed json body and return
      }
      // called only for a 404 (not found) status code:
      response.failure = { resp ->
        println "My response handler got response: ${resp.statusLine}"
        return null
      }
    }
  }

  def getChangeReviewers(changeNumber) {
    def requestPath = "changes/${changeNumber}/reviewers"
    return sendRequest(requestPath)
  }

}