379

I need to make a POST request via cURL from the command line. Data for this request is located in a file. I know that via PUT this could be done with the --upload-file option.

curl host:port/post-file -H "Content-Type: text/xml" --data "contents_of_file"
3

6 Answers 6

537

You're looking for the --data-binary argument:

curl -i -X POST host:port/post-file \
  -H "Content-Type: text/xml" \
  --data-binary "@path/to/file"

In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an @ symbol, so curl knows to read from a file.

9
  • 86
    the @ part is extremely important!
    – Ron Klein
    Aug 17, 2014 at 15:13
  • 13
    actually you can use just -d flag Dec 9, 2016 at 12:26
  • 29
    @Tol182 -d will cause curl to strip newlines, but --data-binary won't.
    – Asclepius
    Jan 12, 2017 at 20:19
  • 3
    For those that can't get it to work, make sure that you are specifying the content type in your headers! Jul 5, 2018 at 17:03
  • 2
    If you're executing the curl command from the same path as your data file, its just --data-binary "@file". May 5, 2020 at 15:39
107

I need to make a POST request via Curl from the command line. Data for this request is located in a file...

All you need to do is have the --data argument start with a @:

curl -H "Content-Type: text/xml" --data "@path_of_file" host:port/post-file-path

For example, if you have the data in a file called stuff.xml then you would do something like:

curl -H "Content-Type: text/xml" --data "@stuff.xml" host:port/post-file-path

The stuff.xml filename can be replaced with a relative or full path to the file: @../xml/stuff.xml, @/var/tmp/stuff.xml, ...

2
  • 4
    In case you need to send a named field, you can also use --data "name@stuff".
    – Hermann
    Sep 3, 2019 at 15:09
  • very slick solution! 100% Dec 2, 2021 at 16:57
25

If you are using form data to upload file,in which a parameter name must be specified , you can use:

curl -X POST -i -F "parametername=@filename" -F "additional_parm=param2" host:port/xxx

0
8

Most of answers are perfect here, but when I landed here for my particular problem, I have to upload binary file (XLSX spread sheet) using POST method, I see one thing missing, i.e. usually its not just file you load, you may have more form data elements, like comment to file or tags to file etc as was my case. Hence, I would like to add it here as it was my use case, so that it could help others.

curl -POST -F comment=mycomment -F file_type=XLSX -F file_data=@/your/path/to/file.XLSX http://yourhost.example.com/api/example_url
4

I was having a similar issue in passing the file as a param. Using -F allowed the file to be passed as form data, but the content type of the file was application/octet-stream. My endpoint was expecting text/csv.

You are able to set the MIME type of the file with the following syntax:

-F 'file=@path/to/file;type=<MIME_TYPE>

So the full cURL command would look like this for a CSV file:

curl -X POST -F 'file=@path/to/file.csv;type=text/csv' https://test.com

There is good documentation on this and other options here: https://catonmat.net/cookbooks/curl/make-post-request#post-form-data

1

I had to use a HTTP connection, because on HTTPS there is default file size limit.

https://techcommunity.microsoft.com/t5/IIS-Support-Blog/Solution-for-Request-Entity-Too-Large-error/ba-p/501134

    curl -i -X 'POST' -F 'file=@/home/testeincremental.xlsx' 'http://example.com/upload.aspx?user=example&password=example123&type=XLSX'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.