简体   繁体   中英

Output bash variable with multiple lines to curl json

I'm trying to create a script that will use the Github API to post a comment containing the output of a command. This output has multiple lines.

Here's what I'm trying to do:

curl -H "Authorization: token oauthtoken" \ 
-H "Content-Type: application/json" \
-X POST -d@- \
https://api.github.com/repos/company/repo/issues/14/comments <<EOF
{
    "body":"$OUTPUT"
}
EOF

How can I output the variable in such a way that it respects the multiple lines contained within? Now when I run that command, all of the newlines get squished on to one line.

I don't think that the basic cause of the problem are the newlines, the issues is that the value of $text is not properly formatted json.

Follow this simple example:

test="
Hello
World
"
curl -X POST -d '{"body": "'"$test"'"}' http://server.com/...

to see new lines working.


To make it possible to send the result of arbitrary commands using json, you need to json-encode the text before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM