Skip to main content

Command Palette

Search for a command to run...

Getting Started with cURL

Updated
3 min read
Getting Started with cURL

Have you ever wondered that how does a browser opens a website in our computer ? How does this complex process occurs within milliseconds and we as a user gets our desired output. For understanding this first we have to gain some knowledge about severs.

How Server works ?

Server is a powerful computer that has 3 main job to do which is

to store data

to process requests

to send response to client

Server is a computer which works 24×7 hours , which is always connected to internet and which handles thousands or sometimes millions of clients.

while using our computer we talk to server many times and we didn’t even notice that thing. when and how we talk to server

  • opening a website

  • login into app

  • watch a video

  • send a message

  • save something

Our computer cannot do everything by itself. Consider our device as a client and server as a remote powerful computer.

How do Clients talk to servers ?

The clients talk to server by using some protocols HTTP/HTTPS , Request and responses and APIs. Every request will be looks like this:

Now the big question arise is how does this requests goes to the server ? do developers send all requests manually ? So the answer is NO , developer doesn’t send requests manually . This is where cURL comes in.

What is cURL ?

cURL means client URL . It is a command line tool that is used to

  • fetch data from URL

  • send request to server

  • test APIs

  • to debug backend services

Why we need cURL ?

cURL is a way to talk with servers without browser. Because browser hides details such as IP,NS servers, etc , browsers can automates all things and are not good for testing APIs. So cURL is used we need it because:

  • fetch data from URL

  • check server response

  • sends GET,POST,PUT,DELETE requests

  • inspect header ,status codes , errors

Sending Request using cURL:

The simplest possible way to fetch a website is

curl http://chaicode.com

it sends an HTTP GET request , downloads the HTML on webpage and prints it out in our terminal.

Understanding Request and Response:

After using that command curl http://chaicode.com cURL generates a request. A request has basically 4 parts;

  1. method => GET

  2. path => /

  3. headers => database

  4. body => to get response for GET

servers receives the request and gives a HTTP Response in the body. Response also has 3 parts :

  1. status code

  2. Headers

  3. Body

How cURL talks to APIs:

API is just a server endpoint that listens for HTTP requests , expects specific methods, body and gives response. cURL is a client that sends those requests exactly as API expects.

Common mistakes beginners make with cURL:

  1. forgetting the HTTP method

  2. not setting content type

  3. wrong quotes (in JSON)

  4. forgetting Authentication Header

  5. not checking status code

  6. ignoring error messages

  7. forgetting -v while debugging

  8. expecting cURL to behave like a browser

  9. copy pasting without understanding


Thank you for reading . Have a happy learning journey. I hope this article added some value for your given precious time.

More from this blog

VrajSoni.Blogs

9 posts