How to perform a POST request using Curl

Key takeaways

  • cURL is a command-line tool used by developers to transfer data between clients and servers using various network protocols.

  • POST requests are used to send data to a server. They are commonly used for submitting forms’ data, APIs, and file uploads.

  • The basic cURL command looks like curl [options] -X POST [URL].

  • Some examples of how we can use cURL are as follows:

    • We can use the curl -X POST [URL] command for simple post requests.

    • We can send data with the curl command using the -d option: curl -X POST [URL] -H "Content-Type: application/json" -d '{"key": "value"}'

    • We can upload files using -F flag, curl -X POST -F "file=@filepath.jpg".

Daniel Stenberg introduced cURL (curl), a command-line tool that enables users to make network requests. The word cURL stands for “Client for URL.” Developers choose cURL because it is available on the main operating systems, including Windows, Linux, and Mac. If not pre-installed, we can use operating system’s package manager to install it easily.

Utilizing cURL for HTTP POST requests—which are used to send data to a server—is one of its most popular applications. This can be useful for tasks such as submitting form data, interacting with APIs, or uploading files.

The curl command for the POST request

To perform a simple POST request, we can use the following command:

curl [options] -X POST [URL] 
  • [options]: This option reprents various flags and parameters to customize the request.
  • -X POST: This specifies that the HTTP method should be POST.
  • [URL]: This is the URL to which the POST request is sent.

Here are some of the commonly used options with curl for making POST requests:

cURL POST Options Overview

Flags

Aliases

Description

Example

-H

--header

Adds a header to the request

-H "Content-Type: application/json"

-d

--data

Adds data to be sent in the request body

--data "data"

-F

--form

Sends data as form field

--form field=data or

--form "file=@/path/to/file

-u

--user

Adds basic authentication credentials

--user username:password

-v

--verbose

Shows detailed information about the request and response

-v

Making a simple POST request

We can make a basic POST request using cURL by using the command below:

curl -X POST https://educative.com/api/

The -X flag specifies a custom request method to use when communicating with the HTTP server. By default, the GET method is used unless some other method is specified.

Posting a request body using cURL

Users can send data with the POST request using the -d flag. It post the request body using the curl command in the following way:

curl -X POST https://educative.com/api/login/\
     -H "Content-Type: application/json" \
     -d '{"name":"Arya", "age":16}'

Sending multiple fields with a POST request

The following POST request sends a user and a pass fields along with their corresponding values.

curl -d "user=user1&pass=abcd" -X POST https://educative.com/api/login

Posting with curl’s -d option will include a default header that looks like: Content-Type: value.

Specifying the Content-Type in POST request

A POST request’s Content-Type header inform the server of the type of data being sent. We can use the -H (or --header) option in curl command to specify the Content-Type header.

Posting JSON data with cURL

The -H flag can be used to send a specific data type or header with cURL. The following command sends a JSON object with the request.

curl -H 'Content-Type: application/json' \ 
     -d '{"name":"Arya", "age":16}'
     https://educative.com/api/login

Sending multipart/form-data in POST request

multipart/form-data is often used for uploading files along with other form data. Use the -F (or --form) option to specify form fields and files.

curl -H "Content-Type: multipart/form-data" \
     -F name=arya \
     -F season=2 \
     -F Rating="5" \
     -X POST \
     https://educative.com/api/data

Sending image data in the POST request

The -F flag is used to specify form fields (including images or files). In the code snippet below, the @ symbol is used to indicate that the data should be read from a file when uploading an image, rather than including the data directly in the request body.

curl -X POST https://educative.com/api/upload \
     -F "image=@/path/to/image.jpg"

Posting XML using cURL

Users can send XML data by specifying the Content-Type header to application/xml or text/xml and use the -d (or --data) option to include the XML content.

curl -X POST https://educative.com/api/data/xml
   -H "Content-Type: application/xml"
   -d " <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>"

Posting form data using cURL

Form data is frequently used in web-based applications and is usually encoded with a URL. Set the Content-Type to application/x-www-form-urlencoded and use the -d option for URL-encoded data.

curl -X POST https://educative.com/api/submit-form \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "username=arya&password=got"

Uploading a file using cURL

We can also send complete files in the command-line using cURL. We can use the command given below:

curl -X POST --form "file=@/pathtoimg.jpg" https://educative.com/api/post

Here, we use --form (or -F) option, and the file=@/pathtoimg.jpg part inform cURL to include the contents of img.png as part of the form data, with the field name file.

Sending basic auth credentials with cURL POST request

To send basic auth credentials with cURL POST request includes the --user username:password option in the curl command. cURL automatically encodes these credentials into a Base64 string and includes them in the request header as Authorization: Basic [token].

curl -X POST https://educative.com/api/login \
     --user "username:password"

Code example

In the following example, we'll use a cURL POST request to interact with API. We use the Fake Store APIIt is open source API that can be used for Pseudo-real data for testing our API calls. to demonstrate the POST request using the curl command. We insert a new item using the POST request and receive an object with a new ID.

Terminal 1
Terminal
Loading...

Quiz!

1

Which option is used to include a header in a curl request?

A)

-F

B)

-d

C)

-H

D)

-X

Question 1 of 50 attempted

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the difference between POST and GET cURL?

cURL POST is used to send data to the server, while cURL GET request is used to get data from the server.


Can we use cURL instead of Postman?

Yes, we can use cURL instead of Postman depending on our requirement. Postman provides a graphical interface for API testing, and cURL allows us to perform similar operations via the command line.


What is a cURL POST?

cURL POST request is used to send data to a server.


What are common key pitfalls when using the cURL POST request?

The following are some of the common key pitfalls when using the cURL POST request:

  • Case sensitivity of the options parameter
  • Missing or incorrect Content-Type Header
  • Handling line breaks incorrectly
  • Improper authentication handling

How to perform a POST request using curl ubuntu

We can perform a POST request using curl command in the following way:

curl -X POST https://educative.com/api/ -H "Content-Type: application/json" -d '{"key":"value"}'

What is Base64?

Base64 is an encoding scheme that converts binary to text in ASCII string format.


What is cURL ssl certificate problem?

An SSL certificate issue occurs when cURL fails to verify the server’s SSL certificate during an API call over HTTPS.


How can we use cURL to follow HTTP redirects?

We can use the -L option in the curl command to automatically follow HTTP redirects.


Copyright ©2024 Educative, Inc. All rights reserved