0

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

0

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

That works. Thank you. It's confusing that the cURL man says: "If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin."
I wanted to delete my question -even though you have been helpful- but some people have decided to start downvoting so I guess I'll just have to deal with it!
@Lorccan any idea why you get downvotes? the only thing i'm conflicted on is "should this question be on stackoverflow.com or is it a superuser.com question?", but that doesn't warrant a downvote, at best it warrants a close/transfer vote
I can't see any reason, but then I asked the question! Not sure I get the distinction between stack overflow and superuser, so perhaps you're right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.