top 10 curl post request examples

Sometimes, you developed APIs in a local machine using spring boot or nodejs framework.

You want to test those APIs in windows or Linux using the curl command.

curl is a command-line tool to issue a request and transfer the data between two machines.

type curl —help to know more about curl options

curl --help

Here is the syntax of the curl post command

curl -X POST [option] [APIURL]

curl is a command-line utility by default works in windows and Linux -X represents the request type i.e GET, POST, PUT, DELETE

Suppose You have an API localhost:8080/api/emp/create which accepts the post request below.

  "id": 1,
  "name": "john"

CURL post request body

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

The data can be json, binary images/pdf,or HTML

If you are sending form data, You have to use the -F option if your data is json format, data can be sent using the -d option.

CURL post request headers

Usually, When you are sending or receiving the data using a post request,

You have to specify the below request headers.

  • content-type - a type of the data user is sending
  • Accept - type of the data server is sending or user received

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 so

curl -X post

It is a basic post request without a header and body to URL

Curl url to send post request json

In this example, We are sending the request.

  • 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