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.
Asked
Active
Viewed 2.9e+01k times
317

Roy Hinkley
- 10,111
- 21
- 80
- 120
4 Answers
526
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

derekerdmann
- 17,696
- 11
- 76
- 110

Jeffrey Froman
- 6,113
- 1
- 16
- 11
-
28If you want to be real fancy you can do: `cat file.txt | curl --data "@-" \`(< url.txt )\`` `@-` tells curl to read from stdin. You could also just use the redirect `(< x.txt )` to put in whatever you want. If you're using bash. – Breedly Apr 29 '15 at 21:44
-
70curl will strip all newlines from the file. If you want to send the file with newlines intact, use `--data-binary` in place of `--data`. – Chris May 14 '15 at 23:27
-
now how would one add login credentials to authorize this request? – fIwJlxSzApHEZIl Jul 20 '17 at 21:14
-
@anon58192932 - That depends upon the security protocol of the server. If you are using `name:value` pairs, like a username and password, then add the necessary headers that match what the service is expecting: `--header: "
: – Roy Hinkley Jul 27 '17 at 18:36"` as a single string.
34
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

cookiedough
- 3,552
- 2
- 26
- 51
-
This one does not convert dots to underscores (. -> _) and keeps newlines. Thanks! – Ramon Fincken Oct 18 '18 at 13:49
-
1It's cool, but it doesn't answer the question since the file is exploded on the command line instead of being specified for curl. I'm also wondering if all of the shell's special characters need to be escaped -- but I don't know enough about that ;) – Gerard ONeill Jun 10 '19 at 14:59
-
4For a huge content file, above curl won't work, `curl: argument list too long` in that case accepted answer is life saver `curl --data "@/path/to/filename" http://...` – Another coder Jun 26 '20 at 02:51
18
In my case, @
caused some sort of encoding problem, I still prefer my old way:
curl -d "$(cat /path/to/file)" https://example.com

Kacifer
- 1,334
- 17
- 26
-6
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"