top 10 curl post request examples

Sometimes, you develop APIs locally using Spring Boot or Node.js frameworks and need to test those APIs on Windows or Linux using the curl command.

curl is a command-line tool for issuing requests and transferring data between two machines.

To learn more about curl options, type:

type curl --help in the terminal to learn more about it.

curl --help

Here is the syntax of the curl post command

curl -X POST [option] [APIURL]

curl is a command-line utility that works by default in both Windows and Linux. The -X option represents the request type, such as GET, POST, PUT, or DELETE.

Suppose you have an API localhost:8080/api/emp/create that accepts the following POST request.

{
  "id": 1,
  "name": "john"
}

CURL POST Request Body

The request body is the actual data that you are sending to the API.

The data can be in JSON, binary images/PDFs, or HTML.

If you are sending form data, you have to use the -F option. For JSON format data, you can use the -d option.

CURL POST Request Headers

When sending or receiving data using a POST request, you usually need to specify the following request headers.

  • Content-Type: The type of data the user is sending.
  • Accept: The type of data the server is sending or the user is expecting to receive.

When you are sending the data with a post request, You have to specify the type of data that you are sending using content-type.

In CURL, request headers are specified using the -H option.

Basic CURL POST Request with No Data

In this example, we are not sending any data.

curl -X post https://www.mydomain.com

It is a basic POST request without any headers or body to a URL.

Curl URL to Send POST Request JSON

In this example, we are sending the request with the following headers:

Accept: application/json Content-Type: application/json

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' localhost:8080/api/emp/create