JSON body 对于 POST 请求
JSON body for POST request
我正在为 POST 请求构建 body
relativeurl := "this-is-a-test-url"
postBody := fmt.Sprintf("{\"requests\": [{\"httpMethod\": \"GET\",\"relativeUrl\": \"%s\"}]}", relativeurl)
当我执行 postBody
的 fmt.Println
时,我看到:
{
"requests": [
{
"httpMethod": "GET",
"relativeUrl": "this-is-a-test-url"}]}
但是 url 期待 JSON:
{
"requests": [
{
"httpMethod": "GET",
"relativeUrl": "this-is-a-test-url"
}
]
}
我构建 post body 的方式是否错误?
您可以使用json package to handle json inputs and outputs. Use json.Unmarshal function to convert json to golang structure and use json.Marshal函数将golang结构转换为json。
您的两个 JSON 输出示例均有效且功能等效。白色 space 在 JSON 中并不重要。请参阅 JSON.org 中的以下内容:
Whitespace can be inserted between any pair of tokens.
您可以使用 encoding/json
或在线 JSON 解析器轻松测试和格式化 JSON。
但是,您使用的方法很容易出错,因为您的 URL 需要正确转义。例如,如果您的 URL 中有一个双引号 "
,您的代码将产生无效的 JSON.
在 Go 中,最好创建一些结构进行编码。例如:
package main
import (
"encoding/json"
"fmt"
)
type RequestBody struct {
Requests []Request `json:"requests"`
}
type Request struct {
HTTPMethod string `json:"httpMethod"`
RelativeURL string `json:"relativeUrl"`
}
func main() {
body := RequestBody{
Requests: []Request{{
HTTPMethod: "GET",
RelativeURL: "this-is-a-test-url",
}},
}
bytes, err := json.MarshalIndent(body, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
这是一个 运行 示例:
只是提一下正确转义 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)
我正在为 POST 请求构建 body
relativeurl := "this-is-a-test-url"
postBody := fmt.Sprintf("{\"requests\": [{\"httpMethod\": \"GET\",\"relativeUrl\": \"%s\"}]}", relativeurl)
当我执行 postBody
的 fmt.Println
时,我看到:
{
"requests": [
{
"httpMethod": "GET",
"relativeUrl": "this-is-a-test-url"}]}
但是 url 期待 JSON:
{
"requests": [
{
"httpMethod": "GET",
"relativeUrl": "this-is-a-test-url"
}
]
}
我构建 post body 的方式是否错误?
您可以使用json package to handle json inputs and outputs. Use json.Unmarshal function to convert json to golang structure and use json.Marshal函数将golang结构转换为json。
您的两个 JSON 输出示例均有效且功能等效。白色 space 在 JSON 中并不重要。请参阅 JSON.org 中的以下内容:
Whitespace can be inserted between any pair of tokens.
您可以使用 encoding/json
或在线 JSON 解析器轻松测试和格式化 JSON。
但是,您使用的方法很容易出错,因为您的 URL 需要正确转义。例如,如果您的 URL 中有一个双引号 "
,您的代码将产生无效的 JSON.
在 Go 中,最好创建一些结构进行编码。例如:
package main
import (
"encoding/json"
"fmt"
)
type RequestBody struct {
Requests []Request `json:"requests"`
}
type Request struct {
HTTPMethod string `json:"httpMethod"`
RelativeURL string `json:"relativeUrl"`
}
func main() {
body := RequestBody{
Requests: []Request{{
HTTPMethod: "GET",
RelativeURL: "this-is-a-test-url",
}},
}
bytes, err := json.MarshalIndent(body, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
这是一个 运行 示例:
只是提一下正确转义 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)