json - POST 请求的 JSON 正文
问题描述
我正在为 POST 请求构建一个主体
relativeurl := "this-is-a-test-url"
postBody := fmt.Sprintf("{\"requests\": [{\"httpMethod\": \"GET\",\"relativeUrl\": \"%s\"}]}", relativeurl)
当我做 afmt.Println
时postBody
,我看到:
{
"requests": [
{
"httpMethod": "GET",
"relativeUrl": "this-is-a-test-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)
推荐阅读
- c# - 从具有不同名称的旧版本升级设置
- python - 传递的项目数量错误 - 向数据框列添加 numpy 数组内容
- html - 某些部分使用固定宽度的 div
- javascript - JQuery - 添加常规 HTML 和转义 HTML
- c++ - 如何访问推力::device_vector 的成员
- android - 使用查询地理 URI 时 Uri.Builder appendQueryParameter() 和 query() 之间的区别?
- python - 为一个类使用多个构造函数的pythonic方法是什么?
- ssis - SSIS - 由于锁定变量而删除 For Each 循环中的文件错误
- oracle - 运行数据泵导入向导时出现 ORA-39083/ORA-02380/ORA-00959 - 问题 w/dmp 文件?
- arrays - 从另一个数组分配新的变体数组时遇到问题