Join the 2022 Total-Stack Web Dev Bootcamp!


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

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

When it comes to debugging network requests, roll is one of the all-time tools yous can notice.

It's one of those tools that in one case you know how to use you always go 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 curl, swedish, was awarded by the king of Sweden for the contributions that his piece of work (gyre and libcurl) did to the computing world.

Allow's swoop into some of the commands and operations that yous are most probable to desire to perform when working with HTTP requests.

Those examples involve working with HTTP, the nearly pop protocol.

  • Perform an HTTP Get request
  • Become the HTTP response headers
  • Only get the HTTP response headers
  • Perform an HTTP POST request
  • Perform an HTTP POST request sending JSON
  • Perform an HTTP PUT request
  • Follow a redirect
  • Store the response to a file
  • Using HTTP hallmark
  • Prepare a unlike User Agent
  • Inspecting all the details of the asking and the response
  • Copying whatever browser network request to a curl command

Perform an HTTP GET asking

When you perform a request, curl will return the trunk of the response:

            curl https://flaviocopes.com/          

Get the HTTP response headers

By default the response headers are hidden in the output of curl. To bear witness them, use the i choice:

            curl -i https://flaviocopes.com/          

Only get the HTTP response headers

Using the I option, you can get merely the headers, and non the response body:

            curl -I https://flaviocopes.com/          

Perform an HTTP POST request

The Ten pick lets you lot change the HTTP method used. By default, GET is used, and it'due south the same equally writing

            curl -10 Go https://flaviocopes.com/          

Using -X POST will perform a Mail request.

Y'all can perform a POST request passing information URL encoded:

            curl -d              "option=value&something=anothervalue"              -X Mail https://flaviocopes.com/          

In this case, the awarding/x-www-grade-urlencoded Content-Blazon is sent.

Perform an HTTP POST request sending JSON

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

In this case you need to explicitly fix the Content-Type header, by using the H option:

            curlicue -d              '{"selection": "value", "something": "anothervalue"}'              -H              "Content-Type: awarding/json"              -10 Mail https://flaviocopes.com/          

You can also send a JSON file from your disk:

            curl -d              "@my-file.json"              -X POST https://flaviocopes.com/          

Perform an HTTP PUT asking

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

Follow a redirect

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

            ringlet http://flaviocopes.com/          

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

            curl -50 http://flaviocopes.com/          

Shop the response to a file

Using the o selection y'all tin can tell curl to salve the response to a file:

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

You lot can also only save a file by its name on the server, using the O choice:

            gyre -O https://flaviocopes.com/index.html          

Using HTTP hallmark

If a resource requires Bones HTTP Authentication, you tin utilize the u choice to pass the user:countersign values:

            whorl -u user:pass https://flaviocopes.com/          

Gear up a different User Agent

The user agent tells the server which client is performing the asking. By default ringlet sends the ringlet/<version> user agent, like: curl/7.54.0.

You tin specify a different user agent using the --user-agent pick:

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

Inspecting all the details of the asking and the response

Utilise the --verbose selection to make curl output all the details of the asking, and the response:

            whorl --verbose -I https://flaviocopes.com/          
          *   Trying 178.128.202.129... * TCP_NODELAY prepare * Continued to flaviocopes.com (178.128.202.129) port 443 (#0) * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 * Server certificate: flaviocopes.com * Server document: Let's Encrypt Authority X3 * Server certificate: DST Root CA X3 > Caput / HTTP/1.one > Host: flaviocopes.com > User-Agent: scroll/7.54.0 > Accept: */* > < HTTP/1.1 200 OK HTTP/one.i 200 OK < Cache-Command: public, max-historic period=0, must-revalidate Cache-Control: public, max-age=0, must-revalidate < Content-Blazon: text/html; charset=UTF-viii Content-Type: text/html; charset=UTF-8 < Engagement: Mon, 30 Jul 2018 08:08:41 GMT Engagement: Mon, xxx Jul 2018 08:08:41 GMT ...        

Copying whatever browser network request to a curlicue command

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

            curl              'https://github.com/coil/ringlet'              -H              'Connection: keep-alive'              -H              'Pragma: no-cache'              -H              'Cache-Command: no-cache'              -H              'Upgrade-Insecure-Requests: ane'              -H              'DNT: 1'              -H              'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS Ten 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'              -H              'Accept: text/html,application/xhtml+xml,awarding/xml;q=0.ix,image/webp,image/apng,*/*;q=0.8'              -H              'Referer: https://www.google.it/'              -H              'Accept-Encoding: gzip, deflate, br'              -H              'Accept-Language: en-United states of america,en;q=0.9,information technology;q=0.8'              -H              'Cookie: _octo=GH1.1.933116459.1507545550; _ga=GA1.2.643383860.1507545550; tz=Europe%2FRome; user_session=XXXXX; __Host-user_session_same_site=YYYYYY; dotcom_user=flaviocopes; logged_in=aye; has_recent_activity=1; _gh_sess=ZZZZZZ'              --compressed