bash shell 脚本问题,尝试使用 cURL POST 可变 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26614423/
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-03 17:35:50  来源:igfitidea点击:

Trouble with bash shell script, attempting to POST variable JSON data using cURL

jsonbashcurlhipchat

提问by Davey Johnson

I'm having trouble with a bash shell script, attempting to POST variable JSON data using cURL. I'm running from a Mac. I can successfully post static data but I can't seem to figure out how to incorporate variables.

我在使用 bash shell 脚本时遇到问题,尝试使用 cURL POST 可变 JSON 数据。我正在从 Mac 运行。我可以成功发布静态数据,但我似乎无法弄清楚如何合并变量。

I introduced <room> and <token> for the sake of these examples.

为了这些示例,我引入了 <room> 和 <token>。

This script works successfully:

此脚本成功运行:

#!/bin/bash
curl -X POST -H "Content-Type: application/json" --data '{ "color":"red", "message":"Build failed", "message_format":"text" }' https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>

Now, I would like to introduce a formatted date. This script posts successfully, but the "$now" is posted literally: i.e. "Build failed $now" rather than "Build failed 10-28-2014"

现在,我想介绍一个格式化的日期。此脚本成功发布,但“$now”按字面发布:即“Build failed $now”而不是“Build failed 10-28-2014”

#!/bin/bash
now=$(date +"%m-%d-%Y")
curl -X POST -H "Content-Type: application/json" --data '{ "color":"red", "message":"Build failed $now", "message_format":"text" }' https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>

I attempted to format the JSON payload with printf like so. The date string is replaced properly. However, this fails with an error: "The request body cannot be parsed as valid JSON: No JSON object could be decoded: line 1 column 0 (char 0)" - so it seems like I'm misusing $payload.

我试图用 printf 像这样格式化 JSON 有效负载。日期字符串被正确替换。但是,这会失败并出现错误:“请求正文无法解析为有效的 JSON:无法解码 JSON 对象:第 1 行第 0 列(字符 0)” - 所以看起来我在误用 $payload。

#!/bin/bash
now=$(date +"%m-%d-%Y")
payload=$(printf "\'{\"color\":\"red\",\"message\":\"Build failed %s\",\"message_format\":\"text\"}\'" $now)
curl -X POST -H "Content-Type: application/json" --data $payload https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>

Finally, I attempted to eval the entire command. This fails by hanging and it could be that I'm misusing escapes. I've tried many variations of escaping.

最后,我尝试评估整个命令。这因挂起而失败,可能是我滥用了转义。我已经尝试了许多转义的变体。

#!/bin/bash
now=$(date +"%m-%d-%Y")
payload=$(printf "\'{\"color\":\"red\",\"message\":\"Build failed %s\",\"message_format\":\"text\"}\'" $now)
cmd=$(curl -X POST -H \"Content-Type: application\/json\" --data '{\"color\":\"red\",\"message\":\"Build failed $now\",\"message_format\":\"text\"}' https:\/\/api.hipchat.com\/v2\/room\/<room>\/notification?auth_token=<token>)
eval $cmd

I found this questionto be somewhat helpful and I've also read this cURL tutorial. These deal with static data and I think I'm just missing some fundamental bash scripting. Thank you in advance for your help.

我发现这个问题有点帮助,我也阅读了这个cURL 教程。这些处理静态数据,我想我只是缺少一些基本的 bash 脚本。预先感谢您的帮助。

回答by fejese

You just need to use 'and "escape properly:

你只需要正确使用'"转义:

now=$(date +"%m-%d-%Y")
curl -X POST -H "Content-Type: application/json" \
    --data '{ "color":"red", "message":"Build failed '"$now"'", "message_format":"text" }' \
    https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>

or alternatively:

或者:

now=$(date +"%m-%d-%Y")
curl -X POST -H "Content-Type: application/json" \
    --data "{ \"color\":\"red\", \"message\":\"Build failed $now\", \"message_format\":\"text\" }" \
    https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>

Wrapping variables in 'will make bash treat them literally whereas using "will make them replaced by the variable's value

包装变量'将使 bash 按字面意思对待它们,而 using"将使它们被变量的值替换