How to POST file contents using cURL?

According to the last section of -d in man curl:

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. Multiple files can also be specified. Posting data from a file named foobar would thus be done with --data @foobar. When --data is told to read from a file like that, carriage returns and newlines will be stripped out.

That is you don't have to do anything fancy just prepend your filename with a @.


As mentioned in this related question if you want the file uploaded without any changes (curl defaults to stripping carriage-return/line-feed characters) then you may want to use the --data-binary option:

curl -X POST --data-binary @path/to/my-file.txt http://example.com/

To be explicitly clear, the accepted answer suggests:

curl -d "data=@path/to/my-file.txt" http://example.com/

The manual reference is here.

Also see this SE answer and this one also for multi-parts.