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

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

问题描述

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

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

curl:(6)无法解析主机'-d'
curl:(3)[globbing] pos不支持嵌套大括号54
HTTP / 1.1 200 OK
Content-Length: 76
Content-Type:application / json
日期:Wed,29 Jan 2014 19:16:56 GMT

{error:{code: - message:解析错误。},id:null,jsonrpc:2.0}


$ b b

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



这是我的 curl 版本:

  curl 7.30.0(mips-unknown-linux-gnu)libcurl / 7.30.0 OpenSSL / 0.9.8y 
协议:file ftp ftps http https imap imaps pop3 pop3s rtsp smtp smtps tftp
特性:IPv6大文件NTLM NTLM_WB SSL


解决方案

更新:使用更简单

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

而不是我在下面解释。但是,如果它是一个选项,请使用 jq 来生成JSON。这可确保 $ FILENAME 的值已正确引用。

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





$ b b

首先使用请求体的内容定义一个变量会更简单:

 #!/ 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_bodyhttp://192.167.0.13/jsonrpc

但要注意两个大的好处:


  1. 您消除了引号的级别


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

  $(< ...)#bash改进超过$(cat ...)

虽然,你指定了一个进程替换,其中命令的输出就像是一个文件的主体一样。



cat ,从中读取文档。这是包含您的请求正文的here文档。


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"}

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!

This is my curl version:

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 

解决方案

Update: use the simpler

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

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. You eliminate a level of quoting
  2. You can easily format the text for readability.

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.

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中使用字符串变量发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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