bash shell脚本出现问题,尝试使用cURL张贴变量JSON数据 [英] Trouble with bash shell script, attempting to POST variable JSON data using cURL

查看:208
本文介绍了bash shell脚本出现问题,尝试使用cURL张贴变量JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我介绍了< room>和< token>为了这些例子.

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

此脚本成功运行:

#!/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"按字面意思发布:即"Build failed $ now",而不是"Build failed $ 10-2014"

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"

#!/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>

我试图像这样用printf格式化JSON有效负载.日期字符串已正确替换.但是,这失败并显示错误:无法将请求正文解析为有效的JSON:无法解码JSON对象:第1行第0列(字符0)"-似乎我在滥用$ payload.

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.

#!/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

我发现了这个 cURL教程.这些处理静态数据,我想我只是缺少一些基本的bash脚本.预先感谢您的帮助.

I found this question to 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.

推荐答案

您只需要正确使用'"转义即可:

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>

或者:

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>

'中包装变量将使bash对其进行字面处理,而使用"会将其替换为变量的值

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

这篇关于bash shell脚本出现问题,尝试使用cURL张贴变量JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆