curl: pass a named parameter from stdin

2019-07-07 07:19发布

I have a web service that expects some parameters. I want to pass one of these parameters (named "data") to curl via stdin. I tried

echo -n "some data" | curl -d  x="foo" -d y="bar" -d data=@- "http://somewhere"

which doesn't work, as the value of data then is "@-" instead of "some data".

Is it possible to use the @ in curl to associate the input from stdin with a specific parameter?

edit: My goal is to chain multiples web services so the data I pass will be the output of another curl call.

标签: bash curl
3条回答
三岁会撩人
2楼-- · 2019-07-07 07:34

You could also save your data in a variable and then use it like this:

VAR="some data"; curl -d  x="foo" -d y="bar" -d data=$VAR "http://somewhere"
查看更多
迷人小祖宗
3楼-- · 2019-07-07 07:44

Use -d @-

E.g.:

echo '{"text": "Hello **world**!"}' | curl -d @- https://api.github.com/markdown

Output:

<p>Hello <strong>world</strong>!</p>
查看更多
一纸荒年 Trace。
4楼-- · 2019-07-07 07:50

Is this not possible?

curl -d  x="foo" -d y="bar" -d data="@some data" "http://somewhere"

Or

curl -d  x="foo" -d y="bar" -d data="@$(echo "some data")" "http://somewhere"

echo "some data" can be another command or file input: $(<somefile).

查看更多
登录 后发表回答