POSTing Form Data with cURL

In this article, we will show you how to POST Form Data with cURL and also how to POST files using cURL. Developers use cURL to perform operations like Post files, Download content from URL, get stock quotes etc.

What is cURL?

curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP).

POSTing Form Data with cURL

Using curl is very simple and the commands are also straight forward. To post form data using curl start the curl command line with curl-x POST and simply add -F for every form field you want to add to the POST.

curl -X POST -F 'username=admin' -F 'password=domainpassword' http://example.com/post.php

cURL also provides options to send specific data types like JSON or Raw data format. Just pass the content type to the header in cURL statement using -H

# -d to send raw data
curl -X POST -H 'Content-Type: application/json' -d '{"username":"demo","password":"demo"}' http://example.com/login

POSTing Files with cURL

You could post any file types with cURL just by specifying the @ character and the image keyword. Check out the below example which posts the image file with the form data.

curl \
  -F "username=demo" \
  -F "password=demo"\
  -F "filecomment=Posting an Image File" \
  -F "image=@/Desktop/picture.jpg" \
  example.com/uploader.php

If you are using windows then you might need to escape the path of the file using double quotes. Also, if you are uploading any other file type then instead of using images you could use “data=@filepath”.

For posting multipart form data you could pass this in the headers as -H “Content-Type: multipart/form-data” .

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like