Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

--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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...