Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

bash - cURL: Invalid JSON when sending stdin as data

I can control a Philips Hue light with the following command via cURL:

curl -X PUT --data '{"on":true}' "http://<bridgeip>/api/<key>/lights/7/state";

I am generating the payload with a function, so I wanted to pipe it to cURL (to take its input from stdin):

onString='{"on":true}';
echo "$onString" | curl -X PUT --data - "http://<bridgeip>/api/<key>/lights/7/state";

but this throws an error: "body contains invalid json"

What I don't get is that this works:

onString='{"on":true}';
curl -X PUT --data "$onString" "http://<bridgeip>/api/<key>/lights/7/state";

Can anyone explain please?

(Incidentally, when I pipe the output of my function to cat the resultant string is as expected and when copied and pasted into jsonlint checks out as valid JSON.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

--data - doesn't fetch data from stdin, it just sends a literal - ,

to actually fetch data from stdin, use --data @-

(come to think of it, --data-binary @- is probably a better idea, i think it makes a difference with newlines when running on windows, but im not 100% sure)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...