{"id":1696,"date":"2024-07-12T10:23:01","date_gmt":"2024-07-12T14:23:01","guid":{"rendered":"https:\/\/www.peteonsoftware.com\/?p=1696"},"modified":"2024-07-12T10:23:01","modified_gmt":"2024-07-12T14:23:01","slug":"core-tools-to-know-curl","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2024\/07\/12\/core-tools-to-know-curl\/","title":{"rendered":"Core Tools to Know: curl"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/peteonsoftware.com\/images\/202407\/curlytail.jpg\" style=\"float:left;margin:.5rem;\" alt=\"'curl'-y pig's tail\" title=\"'curl'-y pig's tail\">As all bloggers eventually find out, two of the best reasons to write blog posts are either to document something for yourself for later or to force yourself to learn something well enough to explain it to others.  That&#8217;s the impetus of this series that I plan on doing from time to time.  I want to get more familiar with some of these core tools and also have a reference \/ resource available that is in &#8220;Pete Think&#8221; so I can quickly find what I need to use these tools if I forget.  <\/p>\n<p>The first tool I want to tackle is <em>curl<\/em>.  In the world of command-line tools, <em>curl<\/em> shines as a flexible utility for transferring data over various network protocols. Whether you&#8217;re working on the development side, the network admin side, or the security side, learning how to use <em>curl<\/em> effectively can be incredibly beneficial. This blog post will guide you through the very basics of <em>curl<\/em>, cover some common use cases, and explain when <em>curl<\/em> might be a better choice than <em>wget<\/em>.<\/p>\n<h3>What is <em>curl<\/em><\/h3>\n<p><em>curl<\/em> (Client for URL) is a command-line tool for transferring data with URLs. It supports a wide range of protocols including HTTP, HTTPS, FTP, SCP, TELNET, LDAP, IMAP, SMB, and many more. <em>curl<\/em> is known for its flexibility and is widely used for interacting with APIs, downloading files, and testing network connections.<\/p>\n<h3>Installing <em>curl<\/em><\/h3>\n<p>Before diving into <em>curl<\/em> commands, you need to ensure it is installed on your system.  Lots of operating systems come with it.  In fact, even Windows has shipped with <em>curl<\/em> in Windows 10 and 11.<\/p>\n<p><strong>For Linux:<\/strong><\/p>\n<pre>\r\nsudo apt-get install curl  # Debian\/Ubuntu\r\nsudo yum install curl      # CentOS\/RHEL\r\n<\/pre>\n<p><strong>For macOS:<\/strong><\/p>\n<pre>\r\nbrew install curl\r\n<\/pre>\n<p><strong>For Windows<\/strong><br \/>\nYou can download the installer from the official <a href=\"https:\/\/curl.se\/download.html\">curl website<\/a> if it isn&#8217;t already on your system.  To check, just type <em>curl &#8211;help<\/em> at the command prompt and see if it understand the command.  If you get something back like this, you&#8217;re all set.<\/p>\n<pre>\r\nC:\\Users\\peteonsoftware&gt;curl --help\r\nUsage: curl [options...] &lt;url&gt;\r\n -d, --data &lt;data&gt;           HTTP POST data\r\n -f, --fail                  Fail fast with no output on HTTP errors\r\n -h, --help &lt;category&gt;       Get help for commands\r\n -i, --include               Include response headers in output\r\n -o, --output &lt;file&gt;         Write to file instead of stdout\r\n -O, --remote-name           Write output to file named as remote file\r\n -s, --silent                Silent mode\r\n -T, --upload-file &lt;file&gt;    Transfer local FILE to destination\r\n -u, --user &lt;user:password&gt;  Server user and password\r\n -A, --user-agent &lt;name&gt;     Send User-Agent &lt;name&gt; to server\r\n -v, --verbose               Make the operation more talkative\r\n -V, --version               Show version number and quit\r\n\r\nThis is not the full help, this menu is stripped into categories.\r\nUse \"--help category\" to get an overview of all categories.\r\nFor all options use the manual or \"--help all\".\r\n<\/pre>\n<h3>The Most Simple Example<\/h3>\n<p>The simplest way to use <em>curl<\/em> is to fetch the contents of a URL. Here is a basic example that will will print the HTML content of the specified URL to the terminal:<\/p>\n<pre>\r\nc:\\\r\n&lambda; curl https:\/\/hosthtml.live\r\n&lt;!doctype html&gt;\r\n&lt;html data-adblockkey=\"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANDrp2lz7AOmADaN8tA50LsWcjLFyQFcb\/P2Txc58oYOeILb3vBw7J6f4pamkAQVSQuqYsKx3YzdUHCvbVZvFUsCAwEAAQ==_M6heeSY2n3p1IRsqfcIljkNrgqYXDBDFSWeybupIpyihjfHMZhFu8kniDL51hLxUnYHjgmcv2EYUtXfRDcRWZQ==\" lang=\"en\" style=\"background: #2B2B2B;\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\r\n    &lt;link rel=\"icon\" href=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4\/\/8\/AAX+Av7czFnnAAAAAElFTkSuQmCC\"&gt;\r\n    &lt;link rel=\"preconnect\" href=\"https:\/\/www.google.com\" crossorigin&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=\"target\" style=\"opacity: 0\"&gt;&lt;\/div&gt;\r\n&lt;script&gt;window.park = \"eyJ1dWlkIjoiZDFhODUxY2ItOTUyZi00NGUyLTg4ZWMtMmU3ZGNhZmE1OTk0IiwicGFnZV90aW1lIjoxNzIwNzMyMzQxLCJwYWdlX3VybCI6Imh0dHBzOi8vaG9zdGh0bWwubGl2ZS8iLCJwYWdlX21ldGhvZCI6IkdFVCIsInBhZ2VfcmVxdWVzdCI6e30sInBhZ2VfaGVhZGVycyI6e30sImhvc3QiOiJob3N0aHRtbC5saXZlIiwiaXAiOiI3Mi4xMDQuMTY5LjE1NCJ9Cg==\";&lt;\/script&gt;\r\n&lt;script src=\"\/bwjblpHBR.js\"&gt;&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h3>Some Useful Examples to Actually Do Stuff<\/h3>\n<p><strong>Downloading Files<\/strong><\/p>\n<pre>\r\n# To download a file and save it with a specific name:\r\ncurl -o curlypigtail.jpg https:\/\/peteonsoftware.com\/images\/202407\/curlytail.jpg\r\n\r\n# If you want to save the file with the same name as in the URL:\r\ncurl -O https:\/\/peteonsoftware.com\/images\/202407\/curlytail.jpg\r\n<\/pre>\n<p><strong>Sending HTTP Requests<\/strong><\/p>\n<pre>\r\n# GET requests are used to retrieve data from a server. The basic example is already shown above. \r\n# To include headers in the output, use the -i option:\r\ncurl -i https:\/\/hosthtml.live\r\n\r\n# POST requests are used to send data to a server. \r\n# This is particularly useful when interacting with APIs. \r\ncurl -X POST -d \"param1=value1&amp;param2=value2\" http:\/\/somereallygreat.net\/api\r\n\r\n# Many APIs now accept JSON.  This is how you'd send that\r\ncurl -X POST -H \"Content-Type: application\/json\" -d '{\"key1\":\"value1\", \"key2\":\"value2\"}' http:\/\/somereallygreat.net\/api\r\n\r\n# Without explaining it, we included a header above (-H).  That added a Content-Type header.  \r\n# To add an Auth header, you might do something like this\r\ncurl -H \"Authorization: Bearer token\" http:\/\/asitethatneedsbearertokens.com\r\n<\/pre>\n<p><strong>Cookies<\/strong><\/p>\n<pre>\r\n# To save cookies from a response\r\ncurl -c cookies.txt https:\/\/www.google.com\r\n\r\n# To send cookies with a request\r\ncurl -b cookies.txt https:\/\/www.google.com\r\n<\/pre>\n<h3>When to Use <em>curl<\/em> Over <em>wget<\/em><\/h3>\n<p>While both <em>curl<\/em> and <em>wget<\/em> are used to transfer data over the internet, they have different strengths.  Daniel Stenberg is the creator of <em>curl<\/em> (and also contributes to <em>wget<\/em>) and he&#8217;s published a more lengthy comparison <a href=\"https:\/\/daniel.haxx.se\/docs\/curl-vs-wget.html\">here<\/a>.  I defer to the expert, but here are some of my big takeaways.<\/p>\n<p><strong><em>curl<\/em> Advantages<\/strong><\/p>\n<ul>\n<li><strong>Flexibility:<\/strong> <em>curl<\/em> supports a wider range of protocols (like SCP, SFTP) and provides more options for customizing requests.<\/li>\n<li><strong>Availability:<\/strong> <em>curl<\/em> comes preinstalled on macOS and Windows 10\/11.  <em>wget<\/em> doesn&#8217;t.<\/li>\n<li><strong>Pipelining:<\/strong> <em>curl<\/em> can be used to chain multiple requests together, making it powerful for scripting complex interactions.<\/li>\n<li><strong>Reuse:<\/strong> <em>curl<\/em> is a library (libcurl), while <em>wget<\/em> is just a command line tool.<\/li>\n<\/ul>\n<p><strong><em>wget<\/em> Advantages<\/strong><\/p>\n<ul>\n<li><strong>Recursive Downloads:<\/strong> <em>wget<\/em> can download entire websites recursively, making it ideal for mirroring sites.<\/li>\n<li><strong>Simplicity:<\/strong> For simple downloading tasks, <em>wget<\/em> might be more straightforward and easier to use.<\/li>\n<\/ul>\n<p><em>curl<\/em> is a versatile tool that &#8211; once mastered &#8211; can simplify many network-related tasks. From downloading files to interacting with APIs, <em>curl<\/em> provides the flexibility and functionality needed for a wide range of applications. While <em>wget<\/em> has its strengths, particularly for simple downloads and recursive website copying, <em>curl<\/em> shines in its versatility and extensive options for customizing requests.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As all bloggers eventually find out, two of the best reasons to write blog posts are either to document something for yourself for later or to force yourself to learn something well enough to explain &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[151,152,130],"class_list":["post-1696","post","type-post","status-publish","format-standard","hentry","category-general-tips","tag-core-tools-to-know","tag-curl","tag-general-tips"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/1696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=1696"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/1696\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=1696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=1696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=1696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}