I\'m trying to pass the cat
output to curl:
$ cat file | curl --data \'{\"title\":\"mytitle\",\"input\":\"-\"}\' http://api
Bu
# Create the input file
echo -n 'Try
You can use the magical stdin file /dev/stdin
cat data.json | curl -H "Content-Type: application/json" -X POST -d "$(</dev/stdin)" http://api
If you want to type/paste the data without escaping or polluting your bash history then, you can use this
cat | curl -H 'Content-Type: application/json' http://api -d @-
Which drops you into cat
where you can input the data, directly, e.g. Shift + Insert in your terminal. You finish with a newline and a Ctrl + D which signals to cat
that you're done. That data is then passed to curl, and you have a reusable history entry.
Try
curl --data '{"title":"mytitle","input":"'$(cat file)'-"}' http://api
I spent a while trying to figure this out and got it working with the following:
cat data.json | curl -H "Content-Type: application/json" -X POST --data-binary @- http://api
Sounds like you want to wrap the content of input
in a JSON body, and then have that sent over with a POST request. I think that the simplest way to do that is to manipulate stdin first and then push that over to curl using -d @-
. One way could look like this:
cat <(echo '{"title":"mytitle","input":"') file <(echo '"}') \
| curl -d @- http://api
I'm using <(echo)
to use cat
to merge strings and files, but there is almost certainly a better way.
Keep in mind that this does not escape the contents of file
and that you may run into issues because of that.