Terminal

HTTP request on the command-line interface via curl

By March 29, 2021 No Comments
http via curl
1 min read

The command-line interface (CLI) is a text-based interface for performing operations on the operating system of a computer. Some of the popular features of the CLI includes

  1. copying files from one directory to another
  2. Moving files between directories
  3. Navigating from one directory to another
  4. View the list of files in a directory
  5. Editing files in a directory.
  6. Making HTTP requests etc

The CLI allows users to perform operations quickly provided the users know the right commands.

There are various CLI applications, which include GIT, CURL, WGET, GREP, but the focus for today’s article is CURL.

CURL  stands for Client URL Request Library is a tool that makes it possible to perform HTTP requests on the CLI. It is available for Linux, Mac, and Windows OS.

The CURL tool has detailed documentation

Top curl commands for making HTTP requests

1. HTTP Get Request

Method 1

curl https://tolustar.com

Method 2

curl -X GET https://tolustar.com

 

2. HTTP Post Request

Method 1
curl -d '{"name":"Tolustar", "message":"hello"}' \
-H "Content-Type: application/json" \
-X POST https://tolustar.com/contact.php

Method 2
curl -d "name=tolustar&message=hello" -X POST https://tolustar.com/contact.php

3. HTTP Post form data

curl -F "file=@image.jpg" -F "name=tolustar" -F "filedescription=Profile Picture" https://tolustar.com/upload.php

4. HTTP Put Request

Method 1
curl -d '{"name":"Tolustar", "message":"hello"}' \
-H "Content-Type: application/json" \
-X PUT https://tolustar.com/contact.php

Method 2
curl -d "option=value&something=anothervalue" -X PUT https://tolustar.com/

 

5. HTTP Delete Request

curl -X DELETE https://tolustar.com

 

6. User Authentication

curl -u user:pass https://tolustar.com/

7. Save URL to an output file

Method 1

curl https://tolustar.com > index.html

Method 2
curl -o index.html https://tolustar.com

Benefits of curl

Different programming languages and frameworks have different ways of making HTTP requests. In a scenario where you are showing examples of how to make HTTP requests to a particular api instead of wasting time showing how to make HTTP requests in different programming languages such as Javascript, PHP, Ruby, NodeJs, Dart, Python, etc, you can show the HTTP requests using curl examples. It provides a generic and universal way to demonstrate HTTP requests and responses. Users can see the format of the request, including any headers and other parameters required to form the requests. Your users can translate this into a specific format for the language they’re using.

 

In conclusion, I believe you can now make  HTTP requests with curl on your terminal. To know furthermore about curl, visit the documentation here

 

Cheers!!!

Leave a Reply

%d bloggers like this: