Bird
Raised Fist0
Linux CLIscripting~10 mins

curl for HTTP requests in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - curl for HTTP requests
Start curl command
↓
Parse URL and options
↓
Open network connection
↓
Send HTTP request
↓
Receive HTTP response
↓
Display or save response
↓
End
curl starts by reading your command, connects to the web address, sends the request, gets the response, and shows it to you.
Execution Sample
Linux CLI
curl https://example.com
curl -I https://example.com
curl -X POST https://example.com
These commands fetch a webpage, get headers only, and send a POST request respectively.
Execution Table
StepCommand PartActionResult/Output
1curl https://example.comParse URL and default GET methodReady to connect to example.com
2Open network connectionConnect to example.com on port 443Connection established
3Send HTTP GET requestSend GET / HTTP/1.1Request sent
4Receive HTTP responseGet status and content200 OK and HTML content received
5Display responseShow HTML content in terminal<!doctype html>...
6curl -I https://example.comParse URL and HEAD methodReady to connect to example.com
7Open network connectionConnect to example.com on port 443Connection established
8Send HTTP HEAD requestSend HEAD / HTTP/1.1Request sent
9Receive HTTP headersGet status and headers only200 OK and headers received
10Display headersShow headers in terminalHTTP/1.1 200 OK Content-Type: text/html...
11curl -X POST https://example.comParse URL and POST methodReady to connect to example.com
12Open network connectionConnect to example.com on port 443Connection established
13Send HTTP POST requestSend POST / HTTP/1.1Request sent
14Receive HTTP responseGet status and content200 OK or other response
15Display responseShow response content in terminal<response content>
16EndNo more commandscurl exits
💡 curl finishes after receiving and displaying the response or headers.
Variable Tracker
VariableStartAfter 1After 2After 3Final
URLNonehttps://example.comhttps://example.comhttps://example.comhttps://example.com
MethodGET (default)GETHEAD (with -I)POST (with -X POST)POST
ConnectionClosedOpenOpenOpenClosed
ResponseNoneHTML contentHeaders onlyResponse contentDisplayed
Key Moments - 3 Insights
Why does curl show HTML content for the first command but only headers for the second?
Because the first command uses the default GET method which fetches the full page (see execution_table rows 1-5), while the second uses -I which sends a HEAD request to get only headers (rows 6-10).
What does the -X option do in curl?
It changes the HTTP method curl uses to send the request, like POST instead of GET (see execution_table rows 11-15).
Why does curl need to open a network connection each time?
Because it must connect to the server to send the request and receive the response (see connection state in variable_tracker and steps 2,7,12).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what HTTP method does curl use by default when no option changes it?
APOST
BGET
CHEAD
DPUT
💡 Hint
Check the 'Method' variable in variable_tracker and steps 1-5 in execution_table.
At which step does curl send the HTTP HEAD request?
AStep 13
BStep 3
CStep 8
DStep 4
💡 Hint
Look at execution_table rows for the command with -I option.
If you add -X PUT to the curl command, what changes in the execution_table?
AThe HTTP method changes to PUT in the 'Command Part' and 'Action' columns.
BThe URL changes to a different website.
CThe connection step is skipped.
Dcurl will not send any request.
💡 Hint
Refer to how -X POST changed the method in steps 11-15.
Concept Snapshot
curl sends HTTP requests from the command line.
Default method is GET.
Use -I for HEAD requests (headers only).
Use -X to specify other methods like POST.
curl connects, sends request, receives response, then shows it.
Full Transcript
curl is a command line tool to send HTTP requests. When you run curl with a URL, it connects to that address and sends a GET request by default. It then waits for the server to respond and shows you the response content. If you add -I, curl sends a HEAD request instead, which only fetches headers, not the full page. Using -X lets you change the HTTP method, for example to POST. Each time, curl opens a network connection, sends the request, gets the response, and displays it. This process ends when the response is fully received and shown.

Practice

(1/5)
1. What does the curl command primarily do in Linux?
easy
A. Monitor system processes
B. Send HTTP requests to websites
C. Manage file permissions
D. Compile source code

Solution

  1. Step 1: Understand the purpose of curl

    The curl command is designed to send HTTP requests and interact with web servers.
  2. Step 2: Compare with other options

    Compiling code, managing permissions, and monitoring processes are unrelated to curl's function.
  3. Final Answer:

    Send HTTP requests to websites -> Option B
  4. Quick Check:

    curl = HTTP requests [OK]
Hint: curl is for web requests, not system tasks [OK]
Common Mistakes:
  • Confusing curl with system commands like chmod or ps
  • Thinking curl compiles code
  • Assuming curl manages files locally
2. Which of the following is the correct syntax to fetch the content of http://example.com using curl?
easy
A. curl http://example.com
B. curl --head http://example.com
C. curl -x http://example.com
D. curl -d http://example.com

Solution

  1. Step 1: Identify basic curl usage

    To fetch a webpage, simply use curl URL. No extra flags are needed for a simple GET request.
  2. Step 2: Analyze other options

    -x is for proxy, --head fetches headers only, -d sends data (POST), so they are incorrect for simple fetch.
  3. Final Answer:

    curl http://example.com -> Option A
  4. Quick Check:

    Basic fetch = curl URL [OK]
Hint: Use curl followed by URL to fetch page [OK]
Common Mistakes:
  • Using -x which is for proxy
  • Using -d which sends data instead of fetching
  • Using --head which fetches headers only
3. What will be the output of this command?
curl -I https://example.com
medium
A. An error because -I is invalid
B. The full HTML content of the page
C. Only the HTTP headers of the response
D. The page content saved to a file

Solution

  1. Step 1: Understand the -I option in curl

    The -I flag tells curl to fetch only the HTTP headers, not the full content.
  2. Step 2: Check other options

    The full HTML content of the page is full content, which -I does not do. An error because -I is invalid is wrong because -I is valid. The page content saved to a file requires -o flag, not present here.
  3. Final Answer:

    Only the HTTP headers of the response -> Option C
  4. Quick Check:

    -I = headers only [OK]
Hint: Remember -I fetches headers only [OK]
Common Mistakes:
  • Thinking -I fetches full page
  • Confusing -I with -o for output file
  • Assuming -I is invalid flag
4. You want to send JSON data {"name":"John"} to http://api.example.com/users using curl. Which command has an error?
medium
A. curl -d '{name:John}' -X POST -H "Content-Type: application/json" http://api.example.com/users
B. curl -X POST -H "Content-Type: application/json" -d '{"name":"John"}' http://api.example.com/users
C. curl -d '{"name":"John"}' http://api.example.com/users -H "Content-Type: application/json" -X POST
D. curl -X POST -d '{"name":"John"}' -H "Content-Type: application/json" http://api.example.com/users

Solution

  1. Step 1: Check JSON data format

    curl -d '{name:John}' -X POST -H "Content-Type: application/json" http://api.example.com/users uses '{name:John}' which misses double quotes around keys and values, making it invalid JSON. Options A, C, D use correct JSON '{"name":"John"}'.
  2. Step 2: Confirm curl flags order

    Order of flags does not matter in curl, so A, C, D are valid. Only data format in B is wrong.
  3. Final Answer:

    curl -d '{name:John}' -X POST -H "Content-Type: application/json" http://api.example.com/users -> Option A
  4. Quick Check:

    JSON must have quotes [OK]
Hint: JSON keys and values need quotes [OK]
Common Mistakes:
  • Omitting quotes in JSON data
  • Thinking flag order matters in curl
  • Using wrong content-type header
5. You want to download a file from http://example.com/file.zip and save it as myfile.zip. Which curl command does this correctly?
hard
A. curl --save myfile.zip http://example.com/file.zip
B. curl -O http://example.com/file.zip > myfile.zip
C. curl -O myfile.zip http://example.com/file.zip
D. curl -o myfile.zip http://example.com/file.zip

Solution

  1. Step 1: Understand curl file saving options

    -o filename saves the download to the specified filename. -O saves with original name from URL.
  2. Step 2: Analyze each option

    B: curl -O http://example.com/file.zip > myfile.zip saves to 'file.zip' via -O and redirects empty stdout to myfile.zip. C: curl -O myfile.zip http://example.com/file.zip uses -O incorrectly, treating myfile.zip as URL. D: uses invalid flag --save.
  3. Final Answer:

    curl -o myfile.zip http://example.com/file.zip -> Option D
  4. Quick Check:

    -o sets output filename [OK]
Hint: Use -o to name downloaded file [OK]
Common Mistakes:
  • Using > redirection for binary files
  • Confusing -O and -o flags
  • Using non-existent flags like --save
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard