top 10 curl post request examples


Sometimes, you developed API’s in local machine using spring boot or nodejs framework

You want to test those API’s in windows or linux using curl command.

curl is 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 an syntax of curl post command

curl -X POST [option] [APIURL]

curl is an 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 accept post request below

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

CURL post request body

request body is 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 -F option if your data is json format, data can be send using -d option

CURL post request headers

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

You have to specify the below request headers

  • content-type - 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 post request, You have to specify the type of the data that you are sending using content-type

In CURL, request headers are specified using -H option

Basic CURL post request with no data

In this example, We are not sending any data so

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

It is an basic post request with out header and body to URL

Curl url to send post request json

In this example, We are sending 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
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to enable http2 in spring boot application| http Compression example

How to find tomcat version installed| check java version used in tomcat

Solution for com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type

How to enable http2 in tomcat 9 and 10 | Compression configuration to http2

3 ways to Count Number of days between two dates in java| example

How to convert BigDecimal to/from String in java examples

3 Ways to Convert java.sql.date to/from Localdate in java: examples