How to post json data using Curl

This tutorial will show us how to post JSON data using Curl. We will use the git bash terminal to use the curl command. You can download and install git bash from here.

For this example, we have below APIs that we want to test using Curl.

Request methodAPIs/EndPoints
POSThttp://localhost:9092/student/create
PUThttp://localhost:9092/student/update
GEThttp://localhost:9092/student/{id}
DELETEhttp://localhost:9092/student/delete

Note – Check the spring boot crud example using MySQL/PostgreSQL/Oracle.

Performing post operation using Curl

Consider we have an entity called Student.java, having fields id, name, rollNumber, and university. We can post json data using the Curl command.

JSON data

{   
"name":"rakesh1",
"rollNumber":"0126CS071",
"university":"rgtu"
}

URL – http://localhost:9092/student/create

Curl command.

curl -X POST 'http://localhost:9092/student/create' -d '{"name":"rakesh1","rollNumber":"0126CS071","university":"rgtu"}' -H 'Accept: application/json' -H 'content-type: application/json;charset=UTF-8'

Note – We need to add content-type otherwise might get an error that Unsupported Media Type. We can add the content type as content-type: application/json;charset=UTF-8.

Also remember, -H is used for –header and -d is used for –data)

Let’s follow the below steps.

  • Open the git bash terminal
  • use post JSON data curl command
How to post json data using Curl
  • Verify the result

The record has been successfully created.

How to send cookie along with post json data using curl.

We can send other information like cookie and other information also using curl command. Let’s see how to send cookie.

curl -X POST 'http://localhost:9092/student/create' -d '{"name":"rakesh1","rollNumber":"0126CS071","university":"rgtu"}' -H 'Accept: application/json' -H 'content-type: application/json;charset=UTF-8' -H 'Cookie: authToken=aqsdd3333WWWdddddkk'

Performing Get operation using Curl

Let’s see how to perform a GET request using CURL

curl -X GET 'http://localhost:9092/student/7' -H 'Accept: application/json' -H 'content-type: application/json;charset=UTF-8'

Performing PUT operation using Curl

Update command

curl -X PUT 'http://localhost:9092/student/update' -d '{"name":"rakesh2","rollNumber":"0126CS071","university":"rgtu"}' -H 'Accept: application/json' -H 'content-type: application/json;charset=UTF-8'

Performing delete operation using Curl

Same we can create delete request.

That’s all about How to post json data using Curl.

Other examples.