• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Draft / Curl Examples Posting Data and Displaying Headers

Curl Examples Posting Data and Displaying Headers

Last updated on October 1, 2019 by Sal Ferrarello

Curl is a fantastic tool for making web requests from the command line. As a developer, I find this tool particularly useful.

Retrieve Content at a URL

curl https://salferrarello.com/

Making a HEAD Call

Sometimes, we want to see the Header Values returned from a URL using the HEAD.

The HTTP HEAD method requests the headers that are returned if the specified resource would be requested with an HTTP GET method.

curl -I https://salferrarello.com/

Note If you want to see the headers returned for a different call (e.g. POST) see “Display response Headers” below.

Passing Data

Passing Data with Get

With a GET call, we add data as part of the URL.

e.g. https://salferrarello.com/?s=curl

Passing data with Post

We can also pass the data using the POST method instead of the GET method, by adding -d to indicate we are passing data we automatically get a POST (not a GET) call

curl https://salferrarello.com/ -d p=5960

Passing JSON Data

When passing JSON data, we need to inform the web server that we are giving it JSON data by including the appropriate header with -H "Content-Type: application/json"

curl https://salferrarello.com/ -H "Content-Type: application/json" --data '{"username":"xyz","password":"xyz"}'

Display Response Headers

You can dump the response headers to a file for any call using the -D parameter, e.g. curl https://salferrarello.com -D responseHeaders.txt

Display Response Headers on Screen

We can also display the response headers as part of our output (before the content) by using -D -

curl https://salferrarello.com -D -

or

curl POST https://salferrarello.com/ -d p=5960 -D –

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Dev Tips, Draft Tagged With: command line, curl

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in