POST Form Data with cURL
cURL is the magical utility that allows developers to download a URL's content, explore response headers, get stock quotes, confirm our GZip encoding is working, and much more. One more great usage of cUrl for command line is POSTing form data to a server, especially while testing moderate to advanced form processing. And just like other cURL commands, POSTing form data is incredibly simple.
POSTing Form Data with cURL
Start your cURL command with curl -X POST
and then add -F
for every field=value
you want to add to the POST:
curl -X POST -F 'username=davidwalsh' -F 'password=something' http://domain.tld/post-to-me.php
If you were using PHP, you could use print_r
on the $_POST
variable to see that your server received the POST data as expected:
Array( 'username' => 'davidwalsh', 'password' => 'something' )
If you need to send a specific data type or header with cURL, use -H
to add a header:
# -d to send raw data curl -X POST -H 'Content-Type: application/json' -d '{"username":"davidwalsh","password":"something"}' http://domain.tld/login
POSTing Files with cURL
POSTing a file with cURL is slightly different in that you need to add an @
before the file location, after the field name:
curl -X POST -F 'image=@/path/to/pictures/picture.jpg' http://domain.tld/upload
Using PHP to explore the $_FILES
variable array would show file data as though it was uploaded via a form in browser:
Array( "image": array( "name" => "picture.jpg" "type" => "image/jpeg", "tmp_name" => "/path/on/server/to/tmp/phprj5rkG", "error" => 0, "size" => 174476 ) )
POSTing file contents with cURL is Probably easier than you thought, right?
The first time I needed to POST file data from command line I thought I was in for a fight; instead I found that cURL made the process easy!
Very nice artice.! But can you explaint more about different from
-d
option and-F
option in cURL.I was read in manual cURL is
-d
stand for--data
, data when send to form will be endcode. But with-F
( stand forform
), it was really be encode ?Sorry my english.
Ok, I’m working with some hardware, which has an IP address “10.10.12.105”, I can download the file in “*.html” format and view the information. This information is in a table, and now I want to manually edit the information in the table and the upload the revised table back to my hardware.
My cURL Code line for Downloading the file is:
My Revised File is located in
How do I use the cURL to upload my revised “html” file, which is located on my PC?
Thanks for the article! I am posting this here because I scoured the internet for two days to track down an error I thought I had in curl and this is the top hit. It turned out that I never had an error – the -F and -d options both work fine. My problem was that I was pointing my curl POST request at a webpage, e.g., page.php, on which the only content was a form, form.php. Once I changed the request to point at form.php directly it worked like a charm. Dumb, silly error. Hope this saves someone else some time.
I keep on getting “curl: (35) error:0306E06C:bignum routines:BN_mod_inverse:no inverse”. Do you know how I can get around of this? Thanks
Nice and all — but is there a way to post forms through CURL _without_ the data showing on the command-line?
I ask this because I don’t want just anyone on the system who’s fluent enough with the
ps
command to be able to glean potentially sensitive data.Great article! I am trying to post form variables to an iView inside an iFrame. I don’t see the url as it is not displayed. Any suggestions?