Lorccan
Lorccan

Reputation: 823

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

Upvotes: 0

Views: 1681

Answers (1)

hanshenrik
hanshenrik

Reputation: 21463

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

Upvotes: 4

Related Questions