我正在为 POST 请求构建一个主体
relativeurl := "this-is-a-test-url"
postBody := fmt.Sprintf("{\"requests\": [{\"httpMethod\": \"GET\",\"relativeUrl\": \"%s\"}]}", relativeurl)
当我做 a fmt.Println
of 时postBody
,我看到:
{
"requests": [
{
"httpMethod": "GET",
"relativeUrl": "this-is-a-test-url"}]}
但 url 需要一个 JSON:
{
"requests": [
{
"httpMethod": "GET",
"relativeUrl": "this-is-a-test-url"
}
]
}
我构建帖子正文的方式是错误的吗?
只是提到另一种正确转义 JSON 字符串的方法:
// call the json serializer just on the string value :
escaped, _ := json.Marshal(relativeUrl)
// the 'escaped' value already contains its enclosing '"', no need to repeat them here :
body := fmt.Sprintf("{\"requests\": [{\"httpMethod\": \"GET\",\"relativeUrl\": %s}]}", escaped)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句