2 607
With fields:
curl --data "param1=value1¶m2=value2" https://example.com/resource.cgi
With fields specified individually:
curl --data "param1=value1" --data "param2=value2" https://example.com/resource.cgi
Multipart:
curl --form "fileupload=@my-file.txt" https://example.com/resource.cgi
Multipart with fields and a filename:
curl --form "fileupload=@my-file.txt;filename=desired-filename.txt" --form param1=value1 --form param2=value2 https://example.com/resource.cgi
Without data:
curl --data '' https://example.com/resource.cgi
curl -X POST https://example.com/resource.cgi
curl --request POST https://example.com/resource.cgi
For more information see the cURL manual. The cURL tutorial on emulating a web browser is helpful.
With libcurl, use the curl_formadd()
function to build your form before submitting it in the usual way. See the libcurl documentation for more information.
For large files, consider adding parameters to show upload progress:
curl --tr-encoding -X POST -v -# -o output -T filename.dat \
http://example.com/resource.cgi
The -o output
is required, otherwise no progress bar will appear.
517
For a RESTful HTTP POST containing XML:
curl -X POST -d @filename.txt http://example.com/path/to/resource --header "Content-Type:text/xml"
or for JSON, use this:
curl -X POST -d @filename.txt http://example.com/path/to/resource --header "Content-Type:application/json"
This will read the contents of the file named filename.txt
and send it as the post request.
2How can we see response xml not in one line but formatted? – Vitaly Zdanevich – 2016-07-29T13:12:58.930
10I think that you can leave off the -X POST
since that is implied by -d
. – benjifisher – 2016-11-30T19:02:42.043
1How to give multiple headers? – keya – 2017-05-29T11:37:18.987
1Multiple Headers: curl -H "header2:1" -H "header2:2" ... – Tomáš Kratochvíla – 2017-09-08T14:57:24.027
16 @tom-wijsman explanation: curl -X POST
implies an HTTP POST request, the -d
parameter (long version: --data
) tells curl that what follows will be POST parameters, and @filename
designates the contents of the file filename
as parameter. This approach works best with RESTful HTTP APIs as found at Twitter, Facebook, various other web services including Ruby on Rails as well as HTTP APIs of databases such as CouchDB. REST stands for Representational state transfer
137
Data from stdin with -d @-
Example:
echo '{"text": "Hello **world**!"}' | curl -d @- https://api.github.com/markdown
Output:
<p>Hello <strong>world</strong>!</p>
8Great if you have a JSON object already in clipboard – Luca Steeb – 2016-05-29T16:12:31.537
even better: echo "$message" | curl -H "Content-Type: application/json" -d @- "$url" – rzr – 2017-11-08T18:43:29.407
69
curl -d "name=Rafael%20Sagula&phone=3320780" http://www.where.com/guest.cgi
is the example found in the Curl Example Manual.
Use %26 for the ampersands though if the above doesn't work:
curl -d "name=Rafael%20Sagula%26phone=3320780" http://www.where.com/guest.cgi
63
If you want to login to a site, do the following:
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/
The first request saves the session cookie (that is provided upon successful login) in the "headers" file. From now on you can use that cookie to authenticate you to any part of the website that you usually access after logging in with a browser.
8a note from curl's man page: 'The -c, --cookie-jar option is however a better way to store cookies.' – maxschlepzig – 2013-12-28T15:14:27.903
35
curl -v --data-ascii var=value http://example.com
and there are many more options, check curl --help
for more information.
33
If you are lazy, you can get google-chrome or firefox to do all the work for you.
Chrome will copy all the request data in cURL syntax.
Chrome uses --data 'param1=hello¶m2=world'
which you can make more readable by using a single -d
or -F
per parameter depending on which type of POST request you want to send, which can be either application/x-www-form-urlencoded
or multipart/form-data
accordingly.
This will be POST-ed as application/x-www-form-urlencoded
(used for the majority of forms that don't contain file uploads):
curl http://httpbin.org/post \
-H "User-Agent: Mozilla/2.2" \
-d param1=hello \
-d name=dinsdale
For a multipart/form-data
POST use -F
(typically used with forms that contain file uploads, or where order of fields is important, or where multiple fields with the same name are required):
curl http://httpbin.org/post \
-H "User-Agent: Mozilla/2.2" \
-F param1=hello \
-F name=dinsdale \
-F name=piranha
The User-Agent
header is not normally needed, but I've thrown it in just in case. If you need a custom agent then you can avoid having setting it on every request by creating the ~/.curlrc
file which contains e.g. User-Agent: "Mozilla/2.2"
5I'm having trouble understanding... when would I do it
With Fields
, when withMultipart
and whenWithout Data
? – CodyBugstein – 2014-09-21T11:05:30.5539Instead of
--data
you can use-d
. – user35538 – 2015-10-09T16:32:38.3532i have an array of fields. how can i do this? – ARUNBALAN NV – 2016-03-09T13:13:38.577
@ARUNBALANNV convert them into a string first with
implode
– RozzA – 2016-08-31T19:39:43.7809@LauriRanta
--data-urlencode
(no dash), in recent versions at least – waitinforatrain – 2013-02-12T12:34:13.8906Also works if you need to update a resource with a PUT: curl -X PUT ... – Subfuzion – 2014-01-22T04:38:19.640