bash 如何在bash脚本中的curl中将多行json字符串作为正文发布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33161381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:44:59  来源:igfitidea点击:

How to post a multi line json string as body in curl inside bash script

jsonbashcurl

提问by user330612

I have the following bash script

我有以下 bash 脚本

#!/bin/bash

body=$(cat  << EOF
{
    "CreatedBy":  "$(hostname -f)",
    "Id":  "$(uuidgen)",
    "Type":  "TestAlertType",
    "AlertCategory":  "NonUrgent"
    }
EOF
)


curl -H "Content-Type: application/json" -X POST -d $body https://dev.cloudapp.net/v1/

But I get invalid json error on post. What am i missing?

但是我在发布时收到无效的 json 错误。我错过了什么?

回答by user330612

This worked

这有效

curl -H "Content-Type: application/json" -X POST -d "$body" https://dev.cloudapp.net/v1/