CURL HTTP GET, POST,PUT, DELETE Request examples
Today in this article we will see how to Test REST API using CURL Command Line tools. We will see how to execute CURL HTTP GET, POST, PUT, and DELETE Request examples.
We will also see how to call CURL command with Basic Authentication or JWT bearer authentication.
This could be a very legitimate requirement as we deal with multiple services/APIs mostly recently popular REST APIs etc.
These APIs expose their endpoint with responses that sometimes need to be accessible from the Desktop or Exe application or Batch Jobs or Scheduler services.
Today in this article, we will cover below aspects,
Today we will see very basic CURL commands to invoke API or service calls.
We shall also see how to invoke secured API/Service using Authentication credentials like Basic Authentication or Bearer JWT Authentication from a CURL Command.
In this example, I am going to run the CURL command against ASP.NET core 3.1 API but however, but you can run this command against any API or Microservices built using JAVA, Spring framework, etc.
GET/PUT/POST/DELETE CURL commands
Below are a few very basic CURL commands that can be run to invoke API or services.
Curl HTTP GET
Command:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET https://localhost:44317/api/values -k
The response is as below,
content-type: application/json; charset=utf-8
date: Fri, 24 Jan 2020 00:42:58 GMT
server: Microsoft-IIS/10.0
transfer-encoding: chunked
x-powered-by: ASP.NET
Curl HTTP POST API
Command 1:
curl -X POST "https://localhost:44341/api/Values" -H "accept: text/plain" -H "Content-Type: application/json" -d "\"kjhlkjlj\""
Above we are passing a String object as a request body.
Command 2:
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 HTTP PUT
Command:
curl -X PUT "https://localhost:44341/api/Values/1" -H "accept: /" -H "Content-Type: application/json" -d "\"TheCodebuzz\""
Response headers as below,
access-control-allow-origin: * date: Fri, 24 Jan 2020 00:40:18 GMT server: Microsoft-IIS/10.0 transfer-encoding: chunked x-powered-by: ASP.NET
Curl HTTP Delete API
Command:
curl -X DELETE "https://localhost:44341/api/Values/12" -H "accept: */*"
Response headers as below,
access-control-allow-origin: * date: Fri, 24 Jan 2020 00:46:17 GMT server: Microsoft-IIS/10.0 transfer-encoding: chunked x-powered-by: ASP.NET
If you’re dealing with security then you can pass the security tokens required using the Security scheme of your choice.
CURL Basic Authentication
Below is a simple command which you can use against API or service which expects a Basic Authentication credentials.
Command:
curl -X GET "<URL>" -H "accept: text/plain" -H "Authorization: Basic <TOKEN>"
Example:
curl -X GET "https://localhost:44378/Accounts" -H "accept: text/plain" -H "Authorization: Basic ZGZzZGY6c2Rmcw=="
I have executed the commands using Git Bash shell,
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 that you can run against API or service which expects JWT Bearer authentication credential.
Command:
curl -X GET "https://localhost:44341/api/Values" -H "accept: text/plain" -H "Authorization: Bearer <token>
Example:
curl -X GET "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 !!
Summary
Today in this article we learned how to use CURL commands to execute or invoke API/Services for CRUD operations and also learned how to use authentication like Basic or JWT authentication with CURL commands.
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.