0
0
Linux CLIscripting~5 mins

curl for HTTP requests in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to talk to websites or servers from your command line. Curl helps you send and receive data over the internet using simple commands.
When you want to check if a website is up and running by fetching its homepage.
When you need to download a file from a URL without opening a browser.
When you want to send data to a web server, like submitting a form or API request.
When you want to see the headers a server sends back to understand how it responds.
When you want to test an API endpoint quickly from your terminal.
Commands
This command fetches the homepage of example.com and shows the HTML content in your terminal.
Terminal
curl https://example.com
Expected OutputExpected
<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is established to be used for illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html>
This command fetches only the HTTP headers from example.com to see metadata like server type and content length.
Terminal
curl -I https://example.com
Expected OutputExpected
HTTP/1.1 200 OK Age: 604800 Cache-Control: max-age=604800 Content-Type: text/html; charset=UTF-8 Date: Sat, 01 Jan 2022 00:00:00 GMT Etag: "3147526947+ident" Expires: Sat, 08 Jan 2022 00:00:00 GMT Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT Server: ECS (nyb/1D2D) Vary: Accept-Encoding X-Cache: HIT Content-Length: 1256
-I - Fetch only HTTP headers without the body content
This command downloads the homepage of example.com and saves it to a file named example.html.
Terminal
curl -o example.html https://example.com
Expected OutputExpected
No output (command runs silently)
-o - Save the output to a file instead of printing it
This command sends data using POST method to httpbin.org, which echoes back what it received. Useful for testing form submissions.
Terminal
curl -X POST -d "name=John&age=30" https://httpbin.org/post
Expected OutputExpected
{ "args": {}, "data": "", "files": {}, "form": { "age": "30", "name": "John" }, "headers": { "Content-Length": "13", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org" }, "json": null, "origin": "127.0.0.1", "url": "https://httpbin.org/post" }
-X POST - Specify the HTTP method as POST
-d - Send the specified data in the request body
Key Concept

If you remember nothing else from curl, remember: it lets you send and receive web data right from your terminal with simple commands.

Common Mistakes
Forgetting to use quotes around data with special characters in the -d flag
The shell may misinterpret special characters, causing the command to fail or send wrong data.
Always put the data string in double quotes when using -d, like -d "name=John&age=30".
Using curl without -o when you want to save output to a file
Curl prints the content to the terminal, cluttering your screen and not saving the file.
Use -o filename to save the output to a file.
Not specifying -X POST when sending data to an endpoint that requires POST
Curl defaults to GET, so the server may reject or ignore the data sent.
Use -X POST to tell curl to send a POST request.
Summary
Use curl to fetch web pages or data from URLs directly in your terminal.
Use -I to get only headers, and -o to save output to a file.
Use -X and -d flags to send data with POST requests.