Skip to main content

Command Palette

Search for a command to run...

Getting started with cURL

Published
3 min read

Client URL or cURL is a command line tool and provides us with the protocols like HTTP, HTTPS, FTP, SCP, and many more in order to communicate with the servers. But to understand cURL let’s talk about servers.

A server is just a computer whose job is to:

  • receive requests

  • process them

  • send back responses

When you open a website:

  • your browser sends a request to a server

  • the server sends back data (HTML, JSON, images, etc.)

Most of the time, your browser talks to servers for you, but what if you want to talk to a server directly?

That’s where cURL comes in.

It allows our computer to interact with a URL hence name cURL, and allows us to send and receive data from command line which is of a great help and reduce time for programmers.

Why do programmers need cURL?

Programmers use cURL because it helps them:

  • test servers and APIs

  • check if a server is responding

  • debug issues quickly

  • understand what data is being sent and received

It’s like a direct phone call to the server, instead of going through a middleman.

First request using cURL

Let’s make our first request using cURL, first we’ll open terminal and run a basic get request using curl followed by a URL to retrieve data:

curl https://chaicode.com

This will send an https get request to the specified URL and prints the server’s response which is usually an HTML or JSON code directly in the terminal.

So what just happened here?

Whenever we use curl, two things happen first is request, we send a request that includes where we want to go (URL) and what we want (some data) so we are making a GET request.

Second is response, the data which server sends back, status showing it worked or not, and the data if worked usually in HTML, JSON format.

Using curl to talk to API’s

API’s are just servers that don’t return html but some structured data in JSON. This is how developers test API’s, check responses, and debug backend issues.

cURL helps us see exactly what the server sends back.

GET and POST requests

A GET request simply means “please give me some data”, this is what browser use when we load a webpage or fetch some data from an API. This request is default request.

So whenever we use curl request it simply means open the site in a browser without any UI.

But in POST requests we give some data to curl to work with. Example: curl -X POST https://example.com, we’re telling the server I want to send data, not just receive it.

Common Mistakes:

  1. Trying to learn all flags at once, curl has many options, but we do not need to learn all of them immediately, start with simple URL’s, GET & POST requests.

  2. Do not expect pretty output, it only shows raw responses, no formatting and no styling.

  3. Errors are friends, do not be scared by them, those are the responses servers is telling you something.

Final Thoughts

cURL may look scary at first, but it is actually very simple. It is just a way to send messages to a server from the terminal. You don’t master it in a day, you learn along the way and the confidence builds naturally.