【问题标题】:Read variable in curl request (shell)在 curl 请求中读取变量(shell)
【发布时间】:2018-04-04 13:52:11
【问题描述】:

每当我的服务器上使用推送项目符号出现 nginx 错误时,我都会尝试发送推送通知,curl 请求有效并且我收到通知,但是当我尝试将变量插入“--data-binary”时' 脚本中断的选项。

push() {
 while read str; do
  #echo "$str";
  curl --header 'Access-Token: XXX' \
  -X POST https://api.pushbullet.com/v2/pushes \
  --header 'Content-Type: application/json' \
  --data-binary "{'type': 'note', 'title': 'nginx', 'body': "$str"}"
 done;
}

tail -f /var/log/nginx/error.log | push

我玩过引号,但似乎可以让它工作。有谁知道我哪里出错了?

【问题讨论】:

    标签: shell variables unix curl command


    【解决方案1】:

    你应该只有一个字符串,并且 JSON 数据的双引号应该被转义:

    --data-binary "{\"type\": \"note\", \"title\": \"nginx\", \"body\": \"$str\"}" 
    

    【讨论】:

    • 如果我使用上面的它会返回一个错误。我认为 JSON 必须采用这种格式,外加单引号,内加双引号:--data-binary '{"type": "note", "title": "nginx", "body": " $str"}' 但是这会发送 "$str" 而不是变量的值。引号内引用的变量是否永远不会被评估?
    • 很高兴看到这对您有用:),如果可以解决您的问题,请不要犹豫并接受答案
    • 我已经接受了答案,但我没有足够的代表来表示抱歉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 2017-06-20
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多