Skip to main content

Command Palette

Search for a command to run...

cURLing Your Brain

How to Talk to the Internet Without a Browser using cURL

Updated
4 min read
cURLing Your Brain

Have you ever wondered how powerful a simple black window with a blinking cursor can be? To the uninitiated, it looks like a scene from a 90s hacker movie. But to a developer, that cursor is a gateway.
Whenever you search “google.com”, the browser sends a request to the server, and receives a response that contains all the HTML, CSS and JS to display the website in your browser.

Your browser is just one way to talk to servers. But what if you want to talk to servers without opening a browser? What if you're building an application that needs to fetch data from another service? This is where cURL comes in.

What is cURL?

cURL: Client URL is a command-line tool, can help you to access web data, work with APIs, and other such tasks with only command-line. It helps the client to communicate with the server using text.

Why Do Developers Need cURL?

Here's why cURL is essential for developers:

  1. Testing APIs Without Writing Code: When building or using an API (Application Programming Interface), it can be tested if it works correctly. cURL send test requests without writing a full application first.

  2. Debugging: When something goes wrong with a web application, cURL helps to see exactly what's being sent to the server and what's coming back.

  3. Automation and Scripting: It can be used in scripts to automate tasks like downloading files, checking if a website is up, or fetching data from APIs on a schedule.

  4. Understanding How the Web Works: cURL helps to gain an understanding of how the web works when you click a link or submit a form.

Making Your First Request Using cURL

Most operating systems ships cURL by default, you can check if it is installed by typing curl --version in your terminal. If it is unavailable, follow the installation guide (https://curl.se/download.html) to install.

Now, fire up your terminal for your first command:

curl https://example.com

The moment you press enter, following happens

  • cURL sends a GET request to example.com

  • The server at example.com receives your request

  • The server responds with HTML content of the homepage

  • cURL displays HTML content in your terminal

If you are interested in headers of the webpage:

#Displays headers and HTML content
curl -i https://example.com 

#Displays only headers
curl -I https://example.com

The -i or -I flag tells cURL to include the response headers. These headers contain metadata about the response.

The interaction between the two looks like:

cURL for HTTP Methods

cURL is like “waiters” of the internet that pass data between apps. It is a very powerful tool to communicate to APIs.

APIs use JSON to send and receive data. Learn more about JSON and RESTful API here.

GET- Getting Data

GET can fetch data or resources from the server.

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

The JSON data retrieved looks like:

POST- Creating Request

GET requests ask for data. POST requests send data to the server like submission of the form or make a new entry in the database.

curl -X POST https://jsonplaceholder.typicode.com/posts \
  -H "Content-Type: application/json" \
  -d '{"title": "My First Post", "body": "This is the content", "userId": 1}'

In the command:

  • -X POST: Specifies to use POST Method

  • -H "Content-Type: application/json": sets the HTTP header to indicate JSON data is being sent

  • -d '...': The data being send

PUT & PATCH - Updating the Resource

To update the resources, PUT and PATCH is used.

  • PUT: Replace the entire source

  • PATCH: Modify the data

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

In the command:

  • -X PUT: specifies PUT request.

  • https://jsonplaceholder.typicode.com/posts/1 is the URL of the post to be updated.

  • -H "Content-Type: application/json": sets the content type to JSON.

  • -d '...' : the JSON data with the desired changes.

In case of modification of data:

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

DELETE - Deleting the Resource

DELETE is used to remove the data or record from the server or database.

curl -X DELETE https://jsonplaceholder.typicode.com/posts/1

Try It Yourself: Fire up the terminal and try a few bash commands yourself.

Once you get comfortable with methods, authentication, file uploads, custom headers, and debugging network issues. The simplicity of cURL makes it a very powerful and valuable tool for developers.
PS: Please learn cURL before you touch Postman (This includes Apidog). Happy cURLing !