DEV Community

Cover image for Living in the Shell #17; curl (HTTP/S Client) (Part 2)
Babak K. Shandiz
Babak K. Shandiz

Posted on β€’ Originally published at babakks.github.io on

2 1

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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay