Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use pipe for curl data

Tags:

curl

pipe

I'm trying to pass the cat output to curl:

$ cat file | curl --data '{"title":"mytitle","input":"-"}' http://api 

But input is literally a -.

like image 516
Jürgen Paul Avatar asked Sep 25 '12 13:09

Jürgen Paul


People also ask

How do you send data with curl GET request?

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.

How pass JSON data in curl Post?

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.

How does curl send data into body?

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.

How do I curl a file?

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.


2 Answers

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 
like image 66
Tom Jowitt Avatar answered Sep 19 '22 15:09

Tom Jowitt


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 
like image 20
Nathan Avatar answered Sep 17 '22 15:09

Nathan