[英]Replace JSON body in Curl request from stdin bash
I am trying to fill one variable in the body of a curl request using input from stdin. 我正在尝试使用来自stdin的输入在curl请求的主体中填充一个变量。
echo 123 | curl -d "{\\"query\\": {\\"match\\": {\\"number\\": @- }}}" -XPOST url.com
Unfortunately, the @-
is not being replaced. 不幸的是,
@-
没有被替换。 I would like the body of the request to match the below 我希望请求的内容与以下内容匹配
{
"query": {
"match": {
"number": 123
}
}
}
How can I replace the query.match.number
value from the stdin? 如何替换标准输入中的
query.match.number
值?
curl doesn't read only a subset of a document from stdin, as you appear to be attempting here -- either it reads the entire thing from stdin, or it doesn't read it from stdin. 就像您在这里尝试的那样,curl不会仅从stdin中读取文档的子集-它要么从stdin中读取整个内容,要么不从stdin中读取它。 (If it did what you expect, it would be impossible to put the literal string
@-
in the text of a documented passed to curl -d
without introducing escaping/unescaping behaviors, and thus complicating behavior even further). (如果它达到了您的期望,则不可能在不引入转义/转义行为的情况下,将文字字符串
@-
放入传递给curl -d
的文档文本中,从而使行为进一步复杂化)。
To generate a JSON document that uses a value from stdin, use jq
: 要生成使用stdin中的值的JSON文档,请使用
jq
:
echo 123 |
jq -c '{"query": { "match": { "number": . } } }' |
curl -d @- -XPOST url.com
That said, there's no compelling reason to use stdin here at all. 就是说,根本没有令人信服的理由在这里使用stdin。 Consider instead:
考虑改为:
jq -nc --arg number '123' \
'{"query": { "match": { "number": ($number | tonumber) } } }' |
curl -d @- -XPOST url.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.