Skip to main content

Command Palette

Search for a command to run...

Getting Started with cURL

Published
3 min read

Isn't it fascinating how a server, this powerful computer, holds all our data, files, websites, and services? And have you ever wondered how it responds so efficiently whenever a user makes a request?

Why we need to talk to server ?

  • We need to talk to a server because most internet activities, like streaming videos, sending emails, or browsing, use a server-client model where the client asks and the server replies.

  • To save and store data on the server and get data from it.

  • To connect multiple users.

Important Note:

Servers centralize data management, enabling backups, updates, and protection for millions of users.

What is cURL

cURL is a command-line tool from which we can directly send request /message to server and get response from it

Why programmers need cURL

  • curl is called the best friend of programmers because:

    • We can test if the server is alive or not.

    • We can test and hit the backend directly without any frontend involvement and waiting.

    • It is used for finding where the error occurs so that we can debug.

    • It is used in scripting.

    • It is useful in learning HTTP methods (GET, POST, PUT, DELETE), headers, cookies, and auth tokens.

    • curl is lightweight and used everywhere like Linux, Mac, Windows, etc.

Fetching a webpage using cURL

  1. open the Command Prompt or Powershell and test curl is present in your device or not.

  2. Sending a GET request

    used to get data from the server without changing its state

    We send a request to the server to get data by typing curl https://google.com in the Command Prompt. You will see raw HTML, which is the server's response.

  3. Sending a GET request

    We used to create new and send data to server by using POST request curl-X POST https://www.google.com -d ”test=data”

Understanding request and response

REQUEST

Request means you (client) are sending message or request to server for getting data from server

request involves Method (GET,POST) ,URL(google.com), HEADER(extra information), BODY(data )

RESPONSE

Response means reply from server to client which involve Status code (200,404,500),Header(response info), Html page(HTML/JSON)

Using cURL to talk to APIs

1 ) GET request (data fetch)

curl https://api.github.com/users

2 ) POST request (data send)

curl -X POST https://api.example.com/login \ -d "username=shiv&password=123"

3 )JSON API call (real-world)

curl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d '{"name":"Shiv","age":22}'

4 ) See full response (debugging)

curl -X POST https://api.example.com/login \ -d "username=shiv&password=123"

Common mistakes beginners make with cURL

  • Forget to add -d with POST

  • Expect output like what a browser shows

  • Forget header when sending JSON

  • Send sensitive data with a GET request

  • Confuse between HTTP and HTTPS

Thanks for reading blogs …. share your feedback.

More from this blog

Full Stack Web dev by Chai Code

7 posts