Home » Questions » Computers [ Ask a new question ]

What is the cURL command-line syntax to do a POST request?

What is the cURL command-line syntax to do a POST request?

How can I make a POST request with the cURL command-line tool?

Asked by: Guest | Views: 244
Total answers/comments: 3
Guest [Entry]

"Data from stdin with -d @-

Example:

echo '{""text"": ""Hello **world**!""}' | curl -d @- api.github.com/markdown

Output:

<p>Hello <strong>world</strong>!</p>"
Guest [Entry]

"If you are lazy, you can get google-chrome or firefox to do all the work for you.

Right-click the form you want to submit and select Inspect (or Inspect Element for Firefox). This will open the DevTools panel.
Select the Network tab in devtools and tick the Preserve log checkbox (Persist Logs for firefox).
Submit the form and locate the entry with method POST (right-click on any column header and make sure Method is checked).
Right click the line with POST, and select Copy > Copy as cURL.

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://httpbindotorg/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://httpbindotorg/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 to set it on every request by creating the ~/.curlrc file which contains e.g. User-Agent: ""Mozilla/2.2"""
Guest [Entry]

"curl -v --data-ascii var=value http://example.com

and there are many more options, check curl --help for more information."