How to call REST API using CURL command in Bash script

Last Updated on :April 27, 2024

cURL is a command-line tool and library for transferring data.With cURL, you can make HTTP requests to interact with web services, download files, and perform various network-related tasks. In this blog post, we’ll explore how to call REST APIs using the powerful cURL command-line tool within Bash scripts.

Set Up Your Environment

Ensure you have cURL installed on your system. Most Unix-like operating systems come with cURL pre-installed. You can verify its presence by running curl –version in your terminal.

Construct the cURL Command

Determine the HTTP request method (e.g., GET, POST, PUT, DELETE) required by the API endpoint. Additionally, prepare any data you need to send with the request, such as JSON payloads for POST or PUT requests.

Using the curl command-line tool, construct a command that includes the following components:

  • -X: Specifies the HTTP request method.
  • -H: Sets request headers, such as Content-Type.
  • -d: Send data in the request body.
  • The API endpoint URL.

Execute the cURL Command

Include the constructed cURL command in your Bash script and execute the script to make the API call. Here is an example of curl command with in Bash script with GET, POST, PUT, and DELETE Requests

#!/usr/bin/env bash
#
# AUTHOR: Rakesh Panigrahi
# Call REST APIs with cURL

API_URL="http://localhost:3000"
GET_API_URL="$API_URL/employees"

# GET request
echo "GET Request: $GET_API_URL"
curl -X GET "$GET_API_URL"
echo


POST_API_URL="$API_URL/employees"
# JSON data for POST REQUEST
JSON_DATA='{"name":"Rakesh","email":"[email protected]","department":"IT"}'

echo "POST Request:$POST_API_URL"
curl -X POST \
    -H "Content-Type: application/json" \
    -d "$JSON_DATA" \
    "$POST_API_URL"
echo

PUT_API_URL="$API_URL/employees/3"
# JSON data for PUT REQUEST
JSON_DATA='{"name":"Rakesh Panigrahi","email":"[email protected]","department":"IT"}'


echo "PUT Request:$PUT_API_URL"
curl -X PUT \
    -H "Content-Type: application/json" \
    -d "$JSON_DATA" \
    "$PUT_API_URL"
echo

DELETE_API_URL="$API_URL/employees/4"
echo "DELETE Request:$DELETE_API_URL"
curl -X DELETE "$DELETE_API_URL"
echo

Add authentication to curl command

To add authentication to cURL commands, you typically use the -u option followed by the username and password.


API_URL="http://localhost:3000"
GET_API_URL="$API_URL/employees"

curl -X GET \
    -u "username:password" \
    "$GET_API_URL"
echo

However, passing passwords in plaintext like this is not secure, especially in scripts. Instead, you can use basic authentication with an API key or token. Here’s the example to include authentication using an API key


API_URL="http://localhost:3000"
GET_API_URL="$API_URL/employees"

API_KEY="your_api_key"

# GET request with authentication
curl -X GET \
    -H "Authorization: Bearer $API_KEY" \
    "$GET_API_URL"
echo

In this post , we’ve explored how to call REST APIs using cURL within Bash scripts.
Check out this script on GitHub: GitHub – api-test-curl-with-auth.sh

You may also like...

Leave a Reply

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