I'm trying to pass the cat
output to curl:
$ cat file | curl --data '{"title":"mytitle","input":"-"}' http://api
But input
is literally a -
.
When the -F option is used, curl sends the data using the multipart/form-data Content-Type. Another way to make a POST request is to use the -d option. This causes curl to send the data using the application/x-www-form-urlencoded Content-Type.
To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.
You can pass the body of the POST message to Curl with the -d or --data command-line option. Curl will send data to the server in the same format as the browser when submitting an HTML form. To send binary data in the body of a POST message with Curl, use the --data-binary command-line option.
The basic syntax: Grab file with curl run: $ curl https://your-domain/file.pdf. Get file using ftp or sftp protocol: $ curl ftp://ftp-your-domain-name/file.tar.gz. You can set the output file name while downloading file with the curl, execute: $ curl -o file.
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
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With