Skip to main content

Command Palette

Search for a command to run...

Getting Started with cURL

Updated
2 min read
Getting Started with cURL
J
Aspiring software developer documenting my journey in building, learning, and understanding technology. Computer Science student at NSUT Delhi.

In your web development journey, you will have to test APIs many times, whether they are REST APIs or GraphQL APIs. Though tools like Postman are available for API testing, did you know that you can test APIs directly from your terminal? Yes!

cURL or Client URL is that tool that allows to do that. It is a command line tool used to transfer data to or from a server using URLS. It supports different protocols like HTTP/HTTPS, FTP, SMTP, SFTP, SCP, default one is HTTP.
Basic syntax: curl [options] [url]

curl -I http://example.com

Here is the general response we will get though curl.
This ‘200’ is called as status code
For different types of responses different status code is used
for example:
-200/201 for success,
-404 for network error and
-500 for server error.

Content-type : text/html tells us that the returned data is of type html.
This are some common parts of an API response.

cURL gives different types of commands such as Get, Post, delete etc. Lets see some of its features.
We'll use a https://jsonplaceholder.typicode.com API, this is a free public test api that will help us to learn and understand cURL better.

1.) Get request

curl https://jsonplaceholder.typicode.com/posts

This is the output of all posts listed out, if we want only one we can set ID = 1

curl https://jsonplaceholder.typicode.com/posts/1

Here is single post output:

2.) POST method

curl -X POST https://jsonplaceholder.typicode.com/posts \
     -H "Content-Type: application/json" \
     -d '{"title":"Hello","body":"Learning cURL","userId":1}'

Here we used some flags, lets understand them
-X used to tell http method other than get → ex: -X POST or -X DELETE

-H used to add file header ( meta data of data)
-d used to give data to post
Here is the output:

It might felts overwhelming but if once you go through the commands and try them on your own, the fog will start to clear.
Here is a fun curl command as a gift for reading till now :

curl parrot.live

Hope you your answers and enjoyed the cURL.