Monday, January 27, 2014

How do I make a POST request with cURL command-line tool?
share|improve this question
add comment

migrated from stackoverflow.com Jun 6 '10 at 7:46

This question came from our site for professional and enthusiast programmers.

5 Answers

curl --data "param1=value1&param2=value2" http://example.com/resource.cgi
or
curl --form "fileupload=@filename.txt" http://example.com/resource.cgi
For more information see the cURL manual. You might also find the cURL tutorial on emulating a web browser helpful.
With libcurl, you'd use the curl_formadd() function to build your form before submitting it in the usual way. See the libcurl documentation for more information.
share|improve this answer
 
@LauriRanta --data-urlencode (no dash), in recent versions at least –  waitinforatrain Feb 12 '13 at 12:34
5 
--data '' works if there is no POST data, but the recipient requires a request to be POSTed. – Daniel Beck Apr 27 '13 at 13:15
 
Also works if you need to update a resource with a PUT: curl -X PUT ... –  Subfuzion Jan 22 at 4:38
add comment
For a RESTful HTTP POST containing XML:
curl -X POST -d @filename 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 file named filename.txt and send it as the post request.
share|improve this answer
18 
If your endpoint is expecting text/xml, append --header "Content-Type:text/xml" to your parameters. –  Naftuli Tzvi Kay Oct 19 '11 at 0:16
1 
Could you provide some explanation about what your code does? –  Tom Wijsman Nov 5 '11 at 1:36
10 
If you are sending json, you can specify the content type with: --header "Content-Type:application/json" – Juha Palomäki Mar 20 '12 at 16:13 
2 
@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 –  soundmonster Jun 27 '12 at 11:27
1 
How is this more RESTful than other ways though? –  Niklas Berglund Oct 3 '12 at 15:59
show 1 more comment
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 
share|improve this answer
add comment
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.
share|improve this answer
 
a note from curl's man page: 'The -c, --cookie-jar option is however a better way to store cookies.' – maxschlepzig Dec 28 '13 at 15:14
add comment
curl -v --data-ascii var=value http://example.com
and there are many more options, check curl --help for more information.
share|improve this answer
add comment

6 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes

Zheng junxai5 said...


zhengjx20160721
true religion sale
nike uk
celine bags
michael kors outlet online
oakley sunglasses outlet
adidas boost
oakley outlet
coach outlet canada
beats by dr dre
adidas nmd
kobe 8
nike sb janoski
michael kors outlet
adidas originals
coach factory outlet online
coach factory outlet online
michael kors handbags
nike roshe run
tory burch outlet online
oakley sunglasses
timberlands
coach factory outlet online
coach outlet store online clearances
michael kors outlet
basketball shoes
nike air max uk
tods sale
lebron 12
cheap ray ban sunglasses
coach outlet online
michael kors
jordan retro 11
michael kors outlet clearance
coach outlet store online clearances
air jordan shoes
oakley sunglasses
beats by dre outlet
ray ban sunglasses
oakley outlet
coach outlet clearance

raybanoutlet001 said...

links of london sale
cheap uggs
discount sunglasses
cheap nfl jerseys
true religion sale
michael kors handbags
http://www.raybanglasses.in.net
tiffany jewellery
ralph lauren polo shirts
nike dunks
tiffany online
oakley store online
oakley sunglasses,oakley outlet sunglasses
cheap real jordans
adidas nmd
ray ban uk,cheap ray ban sunglasses
nike zoom
ray ban sunglasses
kobe byrant shoes