Bash 脚本:在 curl JSON Post 数据中使用字符串变量 [英] Bash script: Use string variable in curl JSON Post data

查看:60
本文介绍了Bash 脚本:在 curl JSON Post 数据中使用字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送一个json请求并在post数据中嵌入一个变量.我做了一些研究,我想出了围绕变量的单引号.

I want to send a json request and embedd a variable in the post data. I did a little research and I came up with the single quotes around the variable.

    #!/bin/bash
    FILENAME="/media/file.avi"
    curl -i -X POST -H "Content-Type: application/json" —d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"file":"'$FILENAME'"}}}' http://192.167.0.13/jsonrpc

不幸的是,我遇到了一些错误:

Unfortunately I get some errors:

curl: (6) Couldn't resolve host '—d'
curl: (3) [globbing] nested braces not supported at pos 54
HTTP/1.1 200 OK
Content-Length: 76
Content-Type: application/json
Date: Wed, 29 Jan 2014 19:16:56 GMT

{"error":{"code":-32700,"message":"Parse error."},"id":null,"jsonrpc":"2.0"}

显然大括号和http 回答状态存在一些问题,无法执行命令.我这里的代码有什么问题?谢谢!

Appearently there are some problems with the braces and the http answer states, that the command could not be executed. What's wrong with my code here? Thanks!

这是我的 curl 版本:

curl 7.30.0 (mips-unknown-linux-gnu) libcurl/7.30.0 OpenSSL/0.9.8y
Protocols: file ftp ftps http https imap imaps pop3 pop3s rtsp smtp smtps tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL 

推荐答案

更新:使用更简单的

request_body=$(cat <<EOF
{
  "jsonrpc": "2.0",
  "method": "Player.Open",
  "params": {
    "item": {
      "file": "$FILENAME"
    }
  }
}
EOF
)

而不是我在下面解释的内容.但是,如果可以选择,请改用 jq 生成 JSON.这确保了 $FILENAME 的值被正确引用.

rather than what I explain below. However, if it is an option, use jq to generate the JSON instead. This ensures that the value of $FILENAME is properly quoted.

request_body=$(jq -n --arg fname "$FILENAME" '
{
  jsonrpc: "2.0",
  method: "Player.Open",
  params: {item: {file: $fname}}
}'

<小时>

<打击>先用请求正文的内容定义一个变量会更简单:


It would be simpler to define a variable with the contents of the request body first:

#!/bin/bash
header="Content-Type: application/json"
FILENAME="/media/file.avi"
request_body=$(< <(cat <<EOF
{
  "jsonrpc": "2.0",
  "method": "Player.Open",
  "params": {
    "item": {
      "file": "$FILENAME"
    }
  }
}
EOF
))
curl -i -X POST -H "$header" -d "$request_body" http://192.167.0.13/jsonrpc

这个定义可能需要解释一下才能理解,但请注意两大好处:

This definition might require an explanation to understand, but note two big benefits:

  1. 您消除了一定程度的引用
  2. 您可以轻松设置文本格式以提高可读性.

首先,您有一个从文件中读取的简单命令替换:

First, you have a simple command substitution that reads from a file:

$( < ... )   # bash improvement over $( cat ... )

不过,您指定了一个进程替换,而不是文件名,其中命令的输出被用作文件的主体.

Instead of a file name, though, you specify a process substitution, in which the output of a command is used as if it were the body of a file.

进程替换中的命令只是cat,它从here文档中读取.这是包含您的请求正文的此处文档.

The command in the process substitution is simply cat, which reads from a here document. It is the here document that contains your request body.

这篇关于Bash 脚本:在 curl JSON Post 数据中使用字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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