POST 请求的 JSON 正文

萨吉什·克里希南

我正在为 POST 请求构建一个主体

relativeurl := "this-is-a-test-url"

postBody := fmt.Sprintf("{\"requests\": [{\"httpMethod\": \"GET\",\"relativeUrl\": \"%s\"}]}", relativeurl)

当我做 a fmt.Printlnof 时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)

https://play.golang.org/p/WaT-RCnDQuK

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章