The Ubiquity of cURL
If you need to test a REST API, download a file from a server, or debug a broken website, opening a graphical web browser like Chrome or Firefox is often the wrong approach. Browsers hide the underlying mechanics of the HTTP protocol. They automatically handle redirects, cache old data, and format the output, obscuring the raw data exchange.
To interact directly with the web at the protocol level, developers use the curl (Client URL) command. curl is arguably the most widely used network tool in the world. It speaks dozens of protocols (HTTP, HTTPS, FTP, SCP) and allows you to craft precise, highly customized network requests directly from the terminal.
1. The Basic GET Request
The simplest use of curl is a standard GET request. You just provide the URL.
curl https://digitash.com
This command connects to the server and instantly dumps the raw HTML (or JSON) of the page directly into your terminal window.
2. Inspecting Server Headers (The -I Flag)
Often, you don’t care about the HTML body; you want to know if the server is alive and what software it is running. The -I (capital i) flag tells curl to fetch only the HTTP headers.
curl -I https://google.com
The output will show the exact HTTP status code (e.g., HTTP/2 200), the server type (e.g., gws for Google Web Server), and the caching control rules.
3. Testing APIs with POST Requests
When you fill out a web form or submit data to an API, you are making a POST request. You can simulate this entirely with curl using the -X (method) and -d (data) flags.
To send a JSON payload to a mock API endpoint:
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "[email protected]"}'
Notice the -H flag. This allows you to inject custom HTTP headers into the request. In this case, we must tell the receiving server that the data we are sending is formatted as JSON, otherwise the server will reject it.
4. Handling Authentication
Many APIs require a secret token or a username/password to access the data. curl handles this easily.
Basic Authentication
If the server uses classic Basic Auth, use the -u flag:
curl -u username:password https://api.example.com/secure-data
Bearer Tokens
Modern APIs (like Stripe or GitHub) use Bearer tokens passed via the Authorization header. Use the -H flag to inject it:
curl -H "Authorization: Bearer YOUR_SECRET_TOKEN" https://api.example.com/v1/charges
5. Following Redirects
If you curl a URL that has moved (like an old HTTP site redirecting to HTTPS), curl will simply output a 301 Moved Permanently header and stop. It will not automatically follow the redirect like a web browser does.
To force curl to follow the redirect chain to the final destination, use the -L (Location) flag.
curl -L http://example.com
6. Saving Output to a File
If you are downloading a large binary file (like a .zip or a .tar.gz archive), dumping it to the terminal screen will crash your terminal.
Use the -O (capital o) flag to tell curl to save the file to your hard drive using the exact same filename it has on the server.
curl -O https://example.com/software-v2.tar.gz
Conclusion
The curl command strips away the graphical facade of the internet. By allowing you to manually construct GET and POST requests, inject custom headers, and handle raw JSON payloads, it is the absolute standard for debugging APIs and scripting automated web interactions.