cURL: Read from File or Stdin

Redirect or pipe the content of a file or the output of another command as the input of cURL, for example, making a POST request with cURL.

This is done by prefixing the data parameter with @, the rest is the filename to read data from. With -, the data is coming from stdin:

1
2
3
4
5
6
7
8
$ seq 17000 1 17001 | \
curl \
--request POST \
--include \
--header 'Content-Type: application/json' \
--data-binary @- \
--no-buffer \
https://example.com/data

Command seq 17000 1 17001 returns:

1
2
17000
17001

The size of the above two numbers is 12 bytes (including two newlines). But when reading data via @ with --data option, “carriage returns and newlines will be stripped out” [1]. If you check your server, you will see the content length is only 10 bytes.

To correct this problem, we will send the raw data using --data-binary option instead. This preserves \n and \r characters.