Skip to main content

Command Palette

Search for a command to run...

Beginner's Guide to Using cURL

Published
2 min read

What is Server and Why we need to communicate with it?

In simple words a server is a computer on internet that serves.

  • when we open a website we communicate with a server

  • when we login to a app we communicate with a server

To get information from the server we must a request to it and it sends us a response back.

cURL allow us to communicate with the server directly removing browser as a dependency.

What is cURL?

cURL is tool which allow us to send message to a server from the terminal.

  • Browser : talks to the server

  • cURL: talks to server with commands

With the help of cURL, we can:

  • Ask server for data

  • Send data to server

  • Test APIs

Why Programmers Need cURL?

Why we need cURL

  • Browser hides many details

  • App is not fully ready yet

  • No UI for now

What cURL can do

  • Test our API’s

  • Debug server responses

Making Our First Request Using cURL

Example:

curl https://hashnode.com

What happens next

  • cURL will send a request to hashnode.com

  • The hashnode server will reply

  • cURL print the result in our terminal

Understanding Request and Response

Communicating with server is a two step process

Request (us to server)

We can say

  • “Hey server“

  • “I want this resource“

Response (Server to us)

The server replies

  • Status (it worked or not?)

  • Data (HTML, TXT, JSON etc)

The text we see in the terminal is the response body.

Using cURL to Talk to APIs

When we create our own API, we need to check if they actually work.

Why browser does not work

  • It can only make GET requests

  • It hides request and response details

cURL solves this problem.

Making a Request to Our API

We can send request to our API if the our server is running on a specific port in our system.

Example: curl http://localhost:3000

If API is working

  • Server sends a response

  • cURL prints it in terminal

This confirms API is reachable.

Common Mistakes Beginners Make with cURL

  1. Trying too many flags too early

  2. Expecting Browser like response

  3. Getting Scared by JSON

  4. Ignoring error message

  5. Thinking cURL is for experts

Closing Statement

cURL is a simple tool which allow us

  • How client talks to server

  • How response and request work

  • How APIs behave