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
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • By
    prefers-color-scheme: CSS Media Query

    One device and app feature I've come to appreciate is the ability to change between light and dark modes. If you've ever done late night coding or reading, you know how amazing a dark theme can be for preventing eye strain and the headaches that result.

  • By
    HTML5’s window.postMessage API

    One of the little known HTML5 APIs is the window.postMessage API.  window.postMessage allows for sending data messages between two windows/frames across domains.  Essentially window.postMessage acts as cross-domain AJAX without the server shims. Let's take a look at how window.postMessage works and how you...

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!