Curl POST JSON command examples- Guidelines

curl post json command Curl JWTCURL JWT

Today in this article, we shall see how to execute the Curl POST JSON command.

CURL is a tool to transfer data from or to a server, using different types of protocol protocols.

Today we shall cover below basic scenarios of using curl commands,

Curl POST JSON with string request

Command :

Post example with string object as request body

curl -X POST "your-post-url" -H "accept: text/plain" -H "Content-Type: application/json" -d "\"your string request data\""

In the above command,

-X —-> The HTTP verb indicating operation is GET, POST, PUT or DELETE, etc


d or --data  —-> the data you want to send.

Example 1

Above we are passing String object as a request body.

curl -X POST "https://localhost:44341/api/Values" -H "accept: text/plain" -H "Content-Type: application/json" -d "\"kjhlkjlj\""

Curl POST JSON request

Example2

curl -X POST "https://localhost:44341/api/Values" -H "accept: text/plain" -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}'

Above we are passing JSON data as the request body parameter.

The response header is as below,

 access-control-allow-origin: * 
 content-type: text/plain; charset=utf-8 
 date: Fri, 24 Jan 2020 00:30:12 GMT 
 server: Microsoft-IIS/10.0 
 transfer-encoding: chunked 
 x-powered-by: ASP.NET 

CURL POST JSON using Basic Authentication

Below is a simple command which you can use against API or service which expects Basic Authentication credentials.

Command:

curl -X POST "<URL>" -H "accept: text/plain" -H "Authorization: Basic <TOKEN>"

Example:

curl -X POST "https://localhost:44378/Accounts" -H "accept: text/plain" -H "Authorization: Basic ZGZzZGY6c2Rmcw=="

I have executed the commands using Git Bash shell,

POST JSON data with CURLCurl POST JSON command

One can also pass username and password in the format -u username: password which gets translated to Bas64 string.

CURL JWT Bearer Authentication

Below is a basic command which you can run use against API or service which expects JWT Bearer authentication credential.

Command:

curl -X POST "https://localhost:44341/api/Values" -H "accept: text/plain" -H "Authorization: Bearer <token>

Example:

curl -X POST "https://localhost:44341/api/Values" -H "accept: text/plain" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1Nzk4MzkzODksImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjQ0MzQxIiwiYXVkIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzNDEifQ.6UTixm8bekorxcw0lL8x-mBkNVaIK003LzMtkuIhasZ8"

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *