How to send file contents as body entity using cURL

PostCurl

Post Problem Overview


I am using cURL command line utility to send HTTP POST to a web service. I want to include a file's contents as the body entity of the POST. I have tried using -d </path/to/filename> as well as other variants with type info like --data </path/to/filename> --data-urlencode </path/to/filename> etc... the file is always attached. I need it as the body entity.

Post Solutions


Solution 1 - Post

I believe you're looking for the @filename syntax, e.g.:

strip new lines

curl --data "@/path/to/filename" http://...

keep new lines

curl --data-binary "@/path/to/filename" http://...

curl will strip all newlines from the file. If you want to send the file with newlines intact, use --data-binary in place of --data

Solution 2 - Post

I know the question has been answered, but in my case I was trying to send the content of a text file to the Slack Webhook api and for some reason the above answer did not work. Anywho, this is what finally did the trick for me:

curl -X POST -H --silent --data-urlencode "payload={\"text\": \"$(cat file.txt | sed "s/\"/'/g")\"}" https://hooks.slack.com/services/XXX

Solution 3 - Post

In my case, @ caused some sort of encoding problem, I still prefer my old way:

curl -d "$(cat /path/to/file)" https://example.com

Solution 4 - Post

curl https://upload.box.com/api/2.0/files/3300/content -H "Authorization: Bearer $access_token" -F file=@"C:\Crystal Reports\Crystal Reports\mysales.pdf"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoy HinkleyView Question on Stackoverflow
Solution 1 - PostJeffrey FromanView Answer on Stackoverflow
Solution 2 - PostcookiedoughView Answer on Stackoverflow
Solution 3 - PostwbsnailView Answer on Stackoverflow
Solution 4 - Postuser17221689View Answer on Stackoverflow