cURL 101
Client URL or cURL is a command line tool used to send and receive data from URLs. Basically it helps us send HTTP requests and see or save corresponding HTTP responses from the Command Line.
It is useful to talk to the server directly in a raw fashion without any middleman like Browsers (Chrome/Firefox) or other API tools (Postman/Hoppscotch).
Why to use cURL:
Testing APIs: We can check if your backend is working without building a frontend first.
Automation: We can write a script to download 100 files automatically.
Universal: It works on Windows, Mac, and Linux exactly the same way.
Making the First Request
Sample Request:
curl "https://example.com"
Sample Response:
<!doctype html><html lang="en"><head><title>Example Domain</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>body{background:#eee;width:60vw;margin:15vh auto;font-family:system-ui,sans-serif}h1{font-size:1.5em}div{opacity:0.8}a:link,a:visited{color:#348}</style><body><div><h1>Example Domain</h1><p>This domain is for use in documentation examples without needing permission. Avoid use in operations.<p><a href="https://iana.org/domains/example">Learn more</a></div></body></html>
We sent a request to the server and received the HTML data in raw format from the server.
Anatomy of a cURL command
A typical cURL command looks like this
curl [options] [URL]
| Option | Full Name | Purpose |
-X | --request | Specifies the HTTP method (GET, POST, PUT, DELETE). |
-H | --header | Adds extra information to the request i.e. request header |
-d | --data | Sends data to the server i.e. request body |
-i | --include | Includes the HTTP response headers in the output. |
-u | --user | Provides a username and password for authentication. |
-o | --output | Saves the response to a file instead of showing it in terminal. |
-L | --location | Follows redirects (if a page moved, cURL follows it). |
Examples
- Simple GET request which saves the HTML to a homepage.html file locally.
curl -X GET "https://example.com" -o "homepage.html"
- Simple POST request which uses a request header and sends request body to the server.
curl -X POST "https://jsonplaceholder.typicode.com/posts" \
-H "Content-Type: application/json" \
-d '{"title": "My New Post", "body": "This is the content.", "userId": 1}'
Pro Tip
To get the corresponding cURL command of any request happening in the browser, we can inspect the network tab, right click on any request and copy as curl.
