POST Form Data with cURL

By  on  

cURL is the magical utility that allows developers to download a URL's contentexplore 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!

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • By
    Send Email Notifications for Broken Images Using MooTools AJAX

    One of the little known JavaScript events is the image onError event. This event is triggered when an image 404's out because it doesn't exist. Broken images can make your website look unprofessional and it's important to fix broken images as soon as possible.

Discussion

  1. zhat

    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 for form ), it was really be encode ?
    Sorry my english.

  2. Ron

    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:

    curl -o Camera_xw220.html http://root:Qwe16Sa7@10.10.12.105/oidtable.html
    

    My Revised File is located in

    C:\Users\123098765\Desktop\Camera_ff220-u.html
    

    How do I use the cURL to upload my revised “html” file, which is located on my PC?

  3. JL McKay

    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.

  4. Jin

    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

  5. Sophia

    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.

  6. Jamie

    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?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!