Linux

How to make a POST request with cURL

How to make a POST request with cURL

cURL is a command line utility for transferring data from or to a remote server using one of the supported protocols. It is installed by default on macOS and most Linux distributions.

cURL is used by developers to test APIs, view response headers, and make HTTP requests.

In this article, we will explain how to use cURL to make POST requests. The HTTP POST method is used to send data to a remote server.

Make a POST request

The general form of the curl command to make a POST request is as follows:

curl -X POST [options] [URL]

The -X option determines which HTTP request method to use when communicating with a remote server.

The type of request body is indicated by the Content-Type header.

In general, POST requests are sent via HTML forms. Data sent to the form is encoded in the multipart / form-data or application / x-www-form-urlencoded content type.

To make a POST request, use the -F option, followed by the field = value pair. The following example shows how to make a POST request to a form that has the “name” and “email” fields:

curl -X POST -F 'name=uberhowto' -F 'email=uberhowto@example.com' https://example.com/contact.php

When the -F option is used, curl sends data using a multipart / Content-Type data form.

Another way to make POST requests is to use the -d option. This causes the curl to send data using the Content-Type / x-www-form-urlencoded apps.

curl -X POST -d 'name=uberhowto' -d 'email=uberhowto@example.com' https://example.com/contact.php

If the -d option is used more than once, you can combine data using the & symbol:

curl -X POST -d 'name=uberhowto&email=uberhowto@example.com' https://example.com/contact.php

Determine the Content-Type

To set a specific header or Content Type, use the -H option. The following command sets the POST request type to the / json application and sends the JSON object:

curl -X POST -H "Content-Type: application/json" \    -d '{"name": "uberhowto", "email": "uberhowto@example.com"}' \    https://example/contact

Uploading Files

For POST files with curl, just add the @ symbol before the file location. Files can be in the form of archives, images, documents, etc.

curl -X POST -F 'image=@/home/user/Pictures/wallpaper.jpg' http://example.com/upload

Conclusion

We have shown you how to use curl to make POST requests. For more information on curls, visit the Curls Documentation page.

If you have questions or feedback, don’t hesitate to leave a comment.

Related posts

How to Install Apache Cassandra on Ubuntu 20.04

Linux

How to Install PostgreSQL on Ubuntu 20.04

Linux

How to Install TensorFlow on Ubuntu 20.04

Linux

How to Install and Configure Fail2ban on Ubuntu 20.04

Linux

How to Install Yarn on Ubuntu 20.04

Linux

How to Install Chromium Web Browser on Ubuntu 20.04

Linux

How to Install Docker on Ubuntu 20.04

Linux

How to Install Ruby on Ubuntu 20.04

Linux

How to Install Notepadqq on Ubuntu 20.04, a decent Notepad ++ alternative

Linux