Join the 2022 Full-Stack Web Dev Bootcamp!


curl is a a command line tool that allows to transfer data across the network.

Information technology supports lots of protocols out of the box, including HTTP, HTTPS, FTP, FTPS, SFTP, IMAP, SMTP, POP3, and many more than.

When information technology comes to debugging network requests, curl is one of the best tools you tin discover.

It'due south one of those tools that once you know how to apply yous always get back to. A programmer's best friend.

It's universal, information technology runs on Linux, Mac, Windows. Refer to the official installation guide to install it on your system.

Fun fact: the author and maintainer of scroll, swedish, was awarded past the king of Sweden for the contributions that his work (roll and libcurl) did to the computing globe.

Let's swoop into some of the commands and operations that you are most likely to want to perform when working with HTTP requests.

Those examples involve working with HTTP, the most popular protocol.

  • Perform an HTTP GET asking
  • Get the HTTP response headers
  • But get the HTTP response headers
  • Perform an HTTP Mail request
  • Perform an HTTP Mail service request sending JSON
  • Perform an HTTP PUT asking
  • Follow a redirect
  • Store the response to a file
  • Using HTTP authentication
  • Set a dissimilar User Agent
  • Inspecting all the details of the request and the response
  • Copying any browser network request to a curl command

Perform an HTTP Get asking

When you perform a request, curl volition return the body of the response:

            curl https://flaviocopes.com/          

Get the HTTP response headers

By default the response headers are hidden in the output of coil. To show them, employ the i pick:

            curl -i https://flaviocopes.com/          

Only get the HTTP response headers

Using the I selection, you tin get only the headers, and not the response body:

            curl -I https://flaviocopes.com/          

Perform an HTTP Mail service asking

The 10 selection lets you change the HTTP method used. By default, Go is used, and it's the same as writing

            gyre -Ten GET https://flaviocopes.com/          

Using -X Mail service will perform a Postal service request.

You tin can perform a POST request passing data URL encoded:

            coil -d              "pick=value&something=anothervalue"              -Ten POST https://flaviocopes.com/          

In this instance, the awarding/x-world wide web-form-urlencoded Content-Type is sent.

Perform an HTTP POST request sending JSON

Instead of posting data URL-encoded, like in the example higher up, you might want to send JSON.

In this example you lot need to explicitly fix the Content-Type header, by using the H pick:

            curlicue -d              '{"option": "value", "something": "anothervalue"}'              -H              "Content-Type: application/json"              -X Post https://flaviocopes.com/          

You can also ship a JSON file from your disk:

            coil -d              "@my-file.json"              -10 Postal service https://flaviocopes.com/          

Perform an HTTP PUT request

The concept is the same equally for Mail requests, just change the HTTP method using -X PUT

Follow a redirect

A redirect response like 301, which specifies the Location response header, can be automatically followed by specifying the L pick:

            curl http://flaviocopes.com/          

will not follow automatically to the HTTPS version which I set up upward to redirect to, just this volition:

            ringlet -50 http://flaviocopes.com/          

Store the response to a file

Using the o option you lot can tell curl to save the response to a file:

            curl -o file.html https://flaviocopes.com/          

You can likewise just relieve a file by its name on the server, using the O selection:

            curl -O https://flaviocopes.com/alphabetize.html          

Using HTTP authentication

If a resource requires Bones HTTP Authentication, you tin can use the u option to laissez passer the user:password values:

            curlicue -u user:laissez passer https://flaviocopes.com/          

Set a different User Agent

The user amanuensis tells the server which client is performing the request. By default curl sends the curl/<version> user agent, similar: curl/vii.54.0.

You can specify a different user agent using the --user-amanuensis option:

            scroll --user-agent              "my-user-agent"              https://flaviocopes.com          

Inspecting all the details of the request and the response

Use the --verbose option to make gyre output all the details of the request, and the response:

            curl --verbose -I https://flaviocopes.com/          
          *   Trying 178.128.202.129... * TCP_NODELAY prepare * Connected to flaviocopes.com (178.128.202.129) port 443 (#0) * TLS 1.two connectedness using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 * Server certificate: flaviocopes.com * Server certificate: Permit's Encrypt Authority X3 * Server certificate: DST Root CA X3 > Caput / HTTP/1.ane > Host: flaviocopes.com > User-Agent: gyre/seven.54.0 > Accept: */* > < HTTP/i.ane 200 OK HTTP/1.one 200 OK < Cache-Command: public, max-age=0, must-revalidate Enshroud-Control: public, max-age=0, must-revalidate < Content-Type: text/html; charset=UTF-8 Content-Type: text/html; charset=UTF-8 < Appointment: Mon, xxx Jul 2018 08:08:41 GMT Date: Mon, 30 Jul 2018 08:08:41 GMT ...        

Copying whatsoever browser network request to a ringlet command

When inspecting any network request using the Chrome Developer Tools, you have the option to copy that request to a curl asking:

            scroll              'https://github.com/curl/ringlet'              -H              'Connexion: go on-alive'              -H              'Pragma: no-cache'              -H              'Cache-Control: no-cache'              -H              'Upgrade-Insecure-Requests: ane'              -H              'DNT: 1'              -H              'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'              -H              'Accept: text/html,awarding/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'              -H              'Referer: https://www.google.it/'              -H              'Take-Encoding: gzip, deflate, br'              -H              'Accept-Language: en-U.s.,en;q=0.9,it;q=0.8'              -H              'Cookie: _octo=GH1.one.933116459.1507545550; _ga=GA1.2.643383860.1507545550; tz=Europe%2FRome; user_session=XXXXX; __Host-user_session_same_site=YYYYYY; dotcom_user=flaviocopes; logged_in=yes; has_recent_activity=i; _gh_sess=ZZZZZZ'              --compressed