将GraphQL查询包装在JSON字符串中,以向GraphQL端点发送请求。

huangapple go评论65阅读模式
英文:

Wrapping GraphQL query inside JSON string to send a request to GraphQL endpoint

问题

我正在尝试向一个GraphQL端点发送请求。我的问题是,以下代码返回"Problem parsing JSON"错误:

var jsonStr = []byte(`{ "query": "query { viewer { login } organization ( login:"org") { description } }`)

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

而以下代码则返回了期望的响应:

var jsonStr = []byte(`{ "query": "query { viewer { login } }`)

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

我猜测这可能与空格或双引号有关,但我不确定如何解决这个问题。我之所以将GraphQL查询封装在JSON中,是因为如果不是JSON字符串,bytes.NewBuffer会抛出错误,而我又不能在没有将字符串传递给bytes.NewBuffer之前进行http请求。非常感谢您能提供的任何见解。

英文:

I am trying to send a request to a graphql endpoint. My problem is that

var jsonStr = []byte(`{ "query": "query { viewer { login } organization ( login:"org") { description } }" `) 

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

Returns "Problem parsing JSON" and

var jsonStr = []byte(`{ "query": "query { viewer { login } }" `)

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

Gives me back the desired response. I am assuming that this has to do with something like whitespace or double quotes, but I am unsure exactly how to fix this issue. The only reason I am encasing a GraphQL query inside JSON is because bytes.NewBuffer will throw an error if given anything other than a JSON string and I cannot make the httpRequest without first passing the string into bytes.NewBuffer. Thank you so much for whatever insight you can give me.

答案1

得分: 1

你的jsonStr变量中的JSON无效 - "query"对象内的引号需要转义。

你可以手动转义它们:

var jsonStr = []byte(`{ "query": "query { viewer { login } organization ( login:\"org\") { description } }" `) 

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

或者,如果我在写这段代码,我可能会使用JSON编组来构建这个对象,以避免手动转义JSON:

type QueryRequestBody struct {
	Query string `json:"query"`
}

func main() {
	var query = QueryRequestBody{
		Query: `query { viewer { login } organization ( login:"org") { description } }`,
	}
	jsonStr, err := json.Marshal(query)
	if err != nil {
		panic(err)
	}
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
}
英文:

The json in your jsonStr variable is invalid - the quotes within the "query" object need to be escaped.

You can manually escape them:

var jsonStr = []byte(`{ "query": "query { viewer { login } organization ( login:\"org\") { description } }" `) 

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

Or, if I was writing this, I'd probably use JSON marshaling to construct this object to avoid needing to manually escape the JSON:

type QueryRequestBody struct {
	Query string `json:"query"`
}

func main() {
	var query = QueryRequestBody{
		Query: `query { viewer { login } organization ( login:"org") { description } }`,
	}
	jsonStr, err := json.Marshal(query)
	if err != nil {
		panic(err)
	}
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
}

huangapple
  • 本文由 发表于 2021年6月19日 02:12:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/68039775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定