# curl Command Reference (Updated)
## What it does
Transfer data from or to servers using various protocols (HTTP, HTTPS, FTP, etc.). Essential for API testing and file downloads.
## Basic Usage
```bash
curl https://api.github.com # GET request to URL
curl -o file.zip https://site.com/file.zip # Download file
curl -I https://google.com # HEAD request (headers only)
```
## HTTP Methods (-X flag)
The `-X` flag specifies the HTTP request method:
| Method | Command | Description |
|--------|---------|-------------|
| `GET` | `curl https://api.com/data` | Retrieve data (default - no -X needed) |
| `POST` | `curl -X POST https://api.com/data` | Send/create data |
| `PUT` | `curl -X PUT https://api.com/data` | Update entire resource |
| `PATCH` | `curl -X PATCH https://api.com/data` | Partial update |
| `DELETE` | `curl -X DELETE https://api.com/item/1` | Delete resource |
| `HEAD` | `curl -I https://site.com` | Headers only (use -I instead) |
## Key Flags Explained
| Flag | Full Name | Purpose | Example |
|------|-----------|---------|---------|
| `-X` | `--request` | Set HTTP method | `curl -X POST` |
| `-H` | `--header` | Add custom header | `curl -H "Content-Type: application/json"` |
| `-d` | `--data` | Send data in request body | `curl -d "name=John"` |
| `-o` | `--output` | Save to specific filename | `curl -o myfile.json` |
| `-O` | `--remote-name` | Save with original filename | `curl -O` |
| `-I` | `--head` | HEAD request (headers only) | `curl -I` |
| `-L` | `--location` | Follow redirects | `curl -L` |
| `-s` | `--silent` | No progress bar | `curl -s` |
| `-v` | `--verbose` | Show detailed info | `curl -v` |
## Data Sending Examples
```bash
# Send JSON data (-d flag)
curl -X POST -H "Content-Type: application/json" \
-d '{"username":"john","email":"
[email protected]"}' \
https://api.example.com/users
# Send form data
curl -X POST -d "username=john&password=secret" \
https://site.com/login
# Send data from file
curl -X POST -H "Content-Type: application/json" \
-d @data.json https://api.com/endpoint
```
## Headers (-H flag)
```bash
# Authentication header
curl -H "Authorization: Bearer abc123token" \
https://api.com/protected
# Content type header
curl -H "Content-Type: application/xml" \
-d "<user><name>John</name></user>" \
https://api.com/users
# Multiple headers
curl -H "Accept: application/json" \
-H "User-Agent: MyApp/1.0" \
https://api.com/data
```
## File Operations
```bash
# Download and save with custom name (-o)
curl -o downloaded-file.zip https://site.com/package.zip
# Download with original name (-O)
curl -O https://site.com/package.zip
# Upload file
curl -X POST -F "
[email protected]" \
https://upload.example.com/files
```
## Quick Commands
```bash
# Simple GET (no flags needed)
curl https://api.github.com/users/octocat
# POST with JSON
curl -X POST -H "Content-Type: application/json" \
-d '{"test":"data"}' https://httpbin.org/post
# Download file silently
curl -s -o webpage.html https://example.com
# Check if site is up (just headers)
curl -I https://google.com
# API call with authentication
curl -H "Authorization: Bearer token123" \
https://api.example.com/user/profile
```
## Pro Tips
> - **GET requests don't need `-X GET`** (it's the default)
> - Use `-I` instead of `-X HEAD` for header-only requests
> - `-d` automatically makes it a POST request (no need for `-X POST`)
> - Combine flags: `curl -sL -o file.html https://example.com`