Living in the Shell #17; curl (HTTP/S Client) (Part 2)

babakks

Babak K. Shandiz

Posted on December 16, 2021

Living in the Shell #17; curl (HTTP/S Client) (Part 2)

curl 🌐

Makes requests in various protocols, including (but not limited to) HTTP/HTTPS/FTP/FTPS.

Add a custom header to request -H

curl https://postman-echo.com/get -H 'user-agent: curl'
Enter fullscreen mode Exit fullscreen mode

ℹī¸ You can set any number of headers by repeating -H options.

Make a POST request with JSON body -d & -X

curl https://postman-echo.com/post \
    -X POST \
    -H 'content-type: application/json' \
    -d '{"key":"value"}'
Enter fullscreen mode Exit fullscreen mode
{
  "args": {},
  "data": {
    "key": "value"
  },
  "files": {},
  "form": {},
  "headers": {
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "postman-echo.com",
    "x-amzn-trace-id": "Root=1-61ba2f8f-304430e917ea20e7024a87c3",
    "content-length": "15",
    "user-agent": "curl/7.74.0",
    "accept": "*/*",
    "content-type": "application/json"
  },
  "json": {
    "key": "value"
  },
  "url": "https://postman-echo.com/post"
}

Read request body from file -d@

curl https://postman-echo.com/post \
    -X POST \
    -H 'content-type: application/json' \
    -d @data-file.json
Enter fullscreen mode Exit fullscreen mode

Read request headers from file -H@

curl https://postman-echo.com/post \
    -X POST \
    -H @headers-file.txt \
    -d @data-file.json
Enter fullscreen mode Exit fullscreen mode

⚠ī¸ The headers file should be formatted as "key: value" per line.

Make a HEAD request -I

curl -I https://postman-echo.com/get
Enter fullscreen mode Exit fullscreen mode

ℹī¸ Alternatively, you can use -X HEAD instead of -I.

See more detailed process logs -v

curl -v https://postman-echo.com/get
Enter fullscreen mode Exit fullscreen mode

This will print more low-level details, including protocol handshakes and negotiations.

💖 đŸ’Ē 🙅 🚩
babakks
Babak K. Shandiz

Posted on December 16, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related