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
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Fullscreen API

    As we move toward more true web applications, our JavaScript APIs are doing their best to keep up.  One very simple but useful new JavaScript API is the Fullscreen API.  The Fullscreen API provides a programmatic way to request fullscreen display from the user, and exit...

  • By
    Create Spinning Rays with CSS3 Animations &#038; JavaScript

    Thomas Fuchs, creator of script2 (scriptaculous' second iteration) and Zepto.js (mobile JavaScript framework), creates outstanding animated elements with JavaScript.  He's a legend in his own right, and for good reason:  his work has helped to inspire developers everywhere to drop Flash and opt...

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!