The Computer Oracle

How to POST file contents using cURL?

-------------------------------------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
-------------------------------------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping

--

Chapters
00:00 Question
01:08 Accepted answer (Score 50)
01:44 Answer 2 (Score 38)
02:08 Answer 3 (Score 24)
02:31 Thank you

--

Full question
https://superuser.com/questions/1054742/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #shell #shellscript #curl

#avk47



ACCEPTED ANSWER

Score 53


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 @.




ANSWER 2

Score 41


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/



ANSWER 3

Score 26


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.