CINXE.COM
curl - The Art Of Scripting HTTP Requests Using Curl
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>curl - The Art Of Scripting HTTP Requests Using Curl</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <link rel="stylesheet" type="text/css" href="/curl.css"> <link rel="shortcut icon" href="/favicon.ico"> <link rel="icon" href="/logo/curl-symbol.svg" type="image/svg+xml"> <link rel="alternate" type="application/rss+xml" title="cURL Releases" href="https://github.com/curl/curl/releases.atom"> </head> <body> <div class="main"> <div class="menu"> <a href="/docs/" class="menuitem" title="Documentation Overview">Docs Overview</a> <div class="dropdown"> <a class="dropbtn" href="/docs/projdocs.html">Project</a> <div class="dropdown-content"> <a href="/docs/bugbounty.html">Bug Bounty</a> <a href="/docs/bugs.html">Bug Report</a> <a href="/docs/code-of-conduct.html">Code of conduct</a> <a href="/docs/libs.html">Dependencies</a> <a href="/donation.html">Donate</a> <a href="/docs/faq.html">FAQ</a> <a href="/docs/features.html">Features</a> <a href="/docs/governance.html">Governance</a> <a href="/docs/history.html">History</a> <a href="/docs/install.html">Install</a> <a href="/docs/knownbugs.html">Known Bugs</a> <a href="/logo/">Logo</a> <a href="/docs/todo.html">TODO</a> <a href="/about.html">website Info</a> </div> </div> <div class="dropdown"> <a class="dropbtn" href="/docs/protdocs.html">Protocols</a> <div class="dropdown-content"> <a href="/docs/caextract.html">CA Extract</a> <a href="/docs/http-cookies.html">HTTP cookies</a> <a href="/docs/http3.html">HTTP/3</a> <a href="/docs/mqtt.html">MQTT</a> <a href="/docs/sslcerts.html">SSL certs</a> <a href="/docs/ssl-compared.html">SSL libs compared</a> <a href="/docs/url-syntax.html">URL syntax</a> <a href="/docs/websocket.html">WebSocket</a> </div> </div> <div class="dropdown"> <a class="dropbtn" href="/docs/reldocs.html">Releases</a> <div class="dropdown-content"> <a href="/ch/">Changelog</a> <a href="/docs/security.html">curl CVEs</a> <a href="/docs/releases.html">Release Table</a> <a href="/docs/versions.html">Version Numbering</a> <a href="/docs/vulnerabilities.html">Vulnerabilities</a> </div> </div> <div class="dropdown"> <a class="dropbtn" href="/docs/tooldocs.html">Tool</a> <div class="dropdown-content"> <a href="/docs/comparison-table.html">Comparison Table</a> <a href="/docs/manpage.html">curl man page</a> <a href="/docs/httpscripting.html">HTTP Scripting</a> <a href="/docs/mk-ca-bundle.html">mk-ca-bundle</a> <a href="/docs/tutorial.html">Tutorial</a> <a href="optionswhen.html">When options were added</a> </div> </div> <div class="dropdown"> <a class="dropbtn" href="/docs/whodocs.html">Who and Why</a> <div class="dropdown-content"> <a href="/docs/companies.html">Companies</a> <a href="/docs/copyright.html">Copyright</a> <a href="/sponsors.html">Sponsors</a> <a href="/docs/thanks.html">Thanks</a> <a href="/docs/thename.html">The name</a> </div> </div> </div> <div class="contents"> <div class="where"><a href="/">curl</a> / <a href="/docs/">Docs</a> / <a href="/docs/tooldocs.html">Tool</a> / <b>HTTP Scripting</b></div> <div class="relatedbox"> <b>Related:</b> <br><a href="manpage.html">curl man page</a> <br><a href="faq.html">FAQ</a> <br><a href="tutorial.html">Tutorial</a> </div> <!-- Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. SPDX-License-Identifier: curl --> <h1 id="the-art-of-scripting-http-requests-using-curl">The Art Of Scripting HTTP Requests Using Curl</h1> <h2 id="background">Background</h2> <p>This document assumes that you are familiar with HTML and general networking.</p> <p>The increasing amount of applications moving to the web has made "HTTP Scripting" more frequently requested and wanted. To be able to automatically extract information from the web, to fake users, to post or upload data to web servers are all important tasks today.</p> <p>Curl is a command line tool for doing all sorts of URL manipulations and transfers, but this particular document focuses on how to use it when doing HTTP requests for fun and profit. This documents assumes that you know how to invoke <code>curl --help</code> or <code>curl --manual</code> to get basic information about it.</p> <p>Curl is not written to do everything for you. It makes the requests, it gets the data, it sends data and it retrieves the information. You probably need to glue everything together using some kind of script language or repeated manual invokes.</p> <h2 id="the-http-protocol">The HTTP Protocol</h2> <p>HTTP is the protocol used to fetch data from web servers. It is a simple protocol that is built upon TCP/IP. The protocol also allows information to get sent to the server from the client using a few different methods, as is shown here.</p> <p>HTTP is plain ASCII text lines being sent by the client to a server to request a particular action, and then the server replies a few text lines before the actual requested content is sent to the client.</p> <p>The client, curl, sends an HTTP request. The request contains a method (like GET, POST, HEAD etc), a number of request headers and sometimes a request body. The HTTP server responds with a status line (indicating if things went well), response headers and most often also a response body. The "body" part is the plain data you requested, like the actual HTML or the image etc.</p> <h2 id="see-the-protocol">See the Protocol</h2> <p>Using curl's option <a href="https://curl.se/docs/manpage.html#-v"><code>--verbose</code></a> (<code>-v</code> as a short option) displays what kind of commands curl sends to the server, as well as a few other informational texts.</p> <p><code>--verbose</code> is the single most useful option when it comes to debug or even understand the curl<->server interaction.</p> <p>Sometimes even <code>--verbose</code> is not enough. Then <a href="https://curl.se/docs/manpage.html#-trace"><code>--trace</code></a> and <a href="https://curl.se/docs/manpage.html#--trace-ascii"><code>--trace-ascii</code></a> offer even more details as they show <strong>everything</strong> curl sends and receives. Use it like this:</p> <pre><code>curl --trace-ascii debugdump.txt http://www.example.com/</code></pre> <h2 id="see-the-timing">See the Timing</h2> <p>Many times you may wonder what exactly is taking all the time, or you just want to know the amount of milliseconds between two points in a transfer. For those, and other similar situations, the <a href="https://curl.se/docs/manpage.html#--trace-time"><code>--trace-time</code></a> option is what you need. It prepends the time to each trace output line:</p> <pre><code>curl --trace-ascii d.txt --trace-time http://example.com/</code></pre> <h2 id="see-which-transfer">See which Transfer</h2> <p>When doing parallel transfers, it is relevant to see which transfer is doing what. When response headers are received (and logged) you need to know which transfer these are for. <a href="https://curl.se/docs/manpage.html#--trace-ids"><code>--trace-ids</code></a> option is what you need. It prepends the transfer and connection identifier to each trace output line:</p> <pre><code>curl --trace-ascii d.txt --trace-ids http://example.com/</code></pre> <h2 id="see-the-response">See the Response</h2> <p>By default curl sends the response to stdout. You need to redirect it somewhere to avoid that, most often that is done with <code>-o</code> or <code>-O</code>.</p> <h1 id="url">URL</h1> <h2 id="spec">Spec</h2> <p>The Uniform Resource Locator format is how you specify the address of a particular resource on the Internet. You know these, you have seen URLs like <a href="https://curl.se">https://curl.se</a> or <a href="https://example.com">https://example.com</a> a million times. RFC 3986 is the canonical spec. The formal name is not URL, it is <strong>URI</strong>.</p> <h2 id="host">Host</h2> <p>The hostname is usually resolved using DNS or your /etc/hosts file to an IP address and that is what curl communicates with. Alternatively you specify the IP address directly in the URL instead of a name.</p> <p>For development and other trying out situations, you can point to a different IP address for a hostname than what would otherwise be used, by using curl's <a href="https://curl.se/docs/manpage.html#--resolve"><code>--resolve</code></a> option:</p> <pre><code>curl --resolve www.example.org:80:127.0.0.1 http://www.example.org/</code></pre> <h2 id="port-number">Port number</h2> <p>Each protocol curl supports operates on a default port number, be it over TCP or in some cases UDP. Normally you do not have to take that into consideration, but at times you run test servers on other ports or similar. Then you can specify the port number in the URL with a colon and a number immediately following the hostname. Like when doing HTTP to port 1234:</p> <pre><code>curl http://www.example.org:1234/</code></pre> <p>The port number you specify in the URL is the number that the server uses to offer its services. Sometimes you may use a proxy, and then you may need to specify that proxy's port number separately from what curl needs to connect to the server. Like when using an HTTP proxy on port 4321:</p> <pre><code>curl --proxy http://proxy.example.org:4321 http://remote.example.org/</code></pre> <h2 id="username-and-password">Username and password</h2> <p>Some services are setup to require HTTP authentication and then you need to provide name and password which is then transferred to the remote site in various ways depending on the exact authentication protocol used.</p> <p>You can opt to either insert the user and password in the URL or you can provide them separately:</p> <pre><code>curl http://user:password@example.org/</code></pre> <p>or</p> <pre><code>curl -u user:password http://example.org/</code></pre> <p>You need to pay attention that this kind of HTTP authentication is not what is usually done and requested by user-oriented websites these days. They tend to use forms and cookies instead.</p> <h2 id="path-part">Path part</h2> <p>The path part is just sent off to the server to request that it sends back the associated response. The path is what is to the right side of the slash that follows the hostname and possibly port number.</p> <h1 id="fetch-a-page">Fetch a page</h1> <h2 id="get">GET</h2> <p>The simplest and most common request/operation made using HTTP is to GET a URL. The URL could itself refer to a webpage, an image or a file. The client issues a GET request to the server and receives the document it asked for. If you issue the command line</p> <pre><code>curl https://curl.se</code></pre> <p>you get a webpage returned in your terminal window. The entire HTML document that that URL holds.</p> <p>All HTTP replies contain a set of response headers that are normally hidden, use curl's <a href="https://curl.se/docs/manpage.html#-i"><code>--include</code></a> (<code>-i</code>) option to display them as well as the rest of the document.</p> <h2 id="head">HEAD</h2> <p>You can ask the remote server for ONLY the headers by using the <a href="https://curl.se/docs/manpage.html#-I"><code>--head</code></a> (<code>-I</code>) option which makes curl issue a HEAD request. In some special cases servers deny the HEAD method while others still work, which is a particular kind of annoyance.</p> <p>The HEAD method is defined and made so that the server returns the headers exactly the way it would do for a GET, but without a body. It means that you may see a <code>Content-Length:</code> in the response headers, but there must not be an actual body in the HEAD response.</p> <h2 id="multiple-urls-in-a-single-command-line">Multiple URLs in a single command line</h2> <p>A single curl command line may involve one or many URLs. The most common case is probably to just use one, but you can specify any amount of URLs. Yes any. No limits. You then get requests repeated over and over for all the given URLs.</p> <p>Example, send two GET requests:</p> <pre><code>curl http://url1.example.com http://url2.example.com</code></pre> <p>If you use <a href="https://curl.se/docs/manpage.html#-d"><code>--data</code></a> to POST to the URL, using multiple URLs means that you send that same POST to all the given URLs.</p> <p>Example, send two POSTs:</p> <pre><code>curl --data name=curl http://url1.example.com http://url2.example.com</code></pre> <h2 id="multiple-http-methods-in-a-single-command-line">Multiple HTTP methods in a single command line</h2> <p>Sometimes you need to operate on several URLs in a single command line and do different HTTP methods on each. For this, you might enjoy the <a href="https://curl.se/docs/manpage.html#-:"><code>--next</code></a> option. It is basically a separator that separates a bunch of options from the next. All the URLs before <code>--next</code> get the same method and get all the POST data merged into one.</p> <p>When curl reaches the <code>--next</code> on the command line, it resets the method and the POST data and allow a new set.</p> <p>Perhaps this is best shown with a few examples. To send first a HEAD and then a GET:</p> <pre><code>curl -I http://example.com --next http://example.com</code></pre> <p>To first send a POST and then a GET:</p> <pre><code>curl -d score=10 http://example.com/post.cgi --next http://example.com/results.html</code></pre> <h1 id="html-forms">HTML forms</h1> <h2 id="forms-explained">Forms explained</h2> <p>Forms are the general way a website can present an HTML page with fields for the user to enter data in, and then press some kind of 'OK' or 'Submit' button to get that data sent to the server. The server then typically uses the posted data to decide how to act. Like using the entered words to search in a database, or to add the info in a bug tracking system, display the entered address on a map or using the info as a login-prompt verifying that the user is allowed to see what it is about to see.</p> <p>Of course there has to be some kind of program on the server end to receive the data you send. You cannot just invent something out of the air.</p> <h2 id="get-1">GET</h2> <p>A GET-form uses the method GET, as specified in HTML like:</p> <div class="sourceCode" id="cb14"><pre class="sourceCode html"><code class="sourceCode html"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><form</span> <span class="er">method</span><span class="ot">=</span><span class="st">"GET"</span> <span class="er">action</span><span class="ot">=</span><span class="st">"junk.cgi"</span><span class="kw">></span></span> <span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">text</span> <span class="er">name</span><span class="ot">=</span><span class="st">"birthyear"</span><span class="kw">></span></span> <span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">submit</span> <span class="er">name</span><span class="ot">=</span><span class="st">press</span> <span class="er">value</span><span class="ot">=</span><span class="st">"OK"</span><span class="kw">></span></span> <span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="kw"></form></span></span></code></pre></div> <p>In your favorite browser, this form appears with a text box to fill in and a press-button labeled "OK". If you fill in '1905' and press the OK button, your browser then creates a new URL to get for you. The URL gets <code>junk.cgi?birthyear=1905&press=OK</code> appended to the path part of the previous URL.</p> <p>If the original form was seen on the page <code>www.example.com/when/birth.html</code>, the second page you get becomes <code>www.example.com/when/junk.cgi?birthyear=1905&press=OK</code>.</p> <p>Most search engines work this way.</p> <p>To make curl do the GET form post for you, just enter the expected created URL:</p> <pre><code>curl "http://www.example.com/when/junk.cgi?birthyear=1905&press=OK"</code></pre> <h2 id="post">POST</h2> <p>The GET method makes all input field names get displayed in the URL field of your browser. That is generally a good thing when you want to be able to bookmark that page with your given data, but it is an obvious disadvantage if you entered secret information in one of the fields or if there are a large amount of fields creating a long and unreadable URL.</p> <p>The HTTP protocol then offers the POST method. This way the client sends the data separated from the URL and thus you do not see any of it in the URL address field.</p> <p>The form would look similar to the previous one:</p> <div class="sourceCode" id="cb16"><pre class="sourceCode html"><code class="sourceCode html"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><form</span> <span class="er">method</span><span class="ot">=</span><span class="st">"POST"</span> <span class="er">action</span><span class="ot">=</span><span class="st">"junk.cgi"</span><span class="kw">></span></span> <span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">text</span> <span class="er">name</span><span class="ot">=</span><span class="st">"birthyear"</span><span class="kw">></span></span> <span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">submit</span> <span class="er">name</span><span class="ot">=</span><span class="st">press</span> <span class="er">value</span><span class="ot">=</span><span class="st">" OK "</span><span class="kw">></span></span> <span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a><span class="kw"></form></span></span></code></pre></div> <p>And to use curl to post this form with the same data filled in as before, we could do it like:</p> <pre><code>curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when/junk.cgi</code></pre> <p>This kind of POST uses the Content-Type <code>application/x-www-form-urlencoded</code> and is the most widely used POST kind.</p> <p>The data you send to the server MUST already be properly encoded, curl does not do that for you. For example, if you want the data to contain a space, you need to replace that space with <code>%20</code>, etc. Failing to comply with this most likely causes your data to be received wrongly and messed up.</p> <p>Recent curl versions can in fact url-encode POST data for you, like this:</p> <pre><code>curl --data-urlencode "name=I am Daniel" http://www.example.com</code></pre> <p>If you repeat <code>--data</code> several times on the command line, curl concatenates all the given data pieces - and put a <code>&</code> symbol between each data segment.</p> <h2 id="file-upload-post">File Upload POST</h2> <p>Back in late 1995 they defined an additional way to post data over HTTP. It is documented in the RFC 1867, why this method sometimes is referred to as RFC 1867-posting.</p> <p>This method is mainly designed to better support file uploads. A form that allows a user to upload a file could be written like this in HTML:</p> <pre><code><form method="POST" enctype='multipart/form-data' action="upload.cgi"> <input name=upload type=file> <input type=submit name=press value="OK"> </form></code></pre> <p>This clearly shows that the Content-Type about to be sent is <code>multipart/form-data</code>.</p> <p>To post to a form like this with curl, you enter a command line like:</p> <pre><code>curl --form upload=@localfilename --form press=OK [URL]</code></pre> <h2 id="hidden-fields">Hidden Fields</h2> <p>A common way for HTML based applications to pass state information between pages is to add hidden fields to the forms. Hidden fields are already filled in, they are not displayed to the user and they get passed along just as all the other fields.</p> <p>A similar example form with one visible field, one hidden field and one submit button could look like:</p> <div class="sourceCode" id="cb21"><pre class="sourceCode html"><code class="sourceCode html"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><form</span> <span class="er">method</span><span class="ot">=</span><span class="st">"POST"</span> <span class="er">action</span><span class="ot">=</span><span class="st">"foobar.cgi"</span><span class="kw">></span></span> <span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">text</span> <span class="er">name</span><span class="ot">=</span><span class="st">"birthyear"</span><span class="kw">></span></span> <span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">hidden</span> <span class="er">name</span><span class="ot">=</span><span class="st">"person"</span> <span class="er">value</span><span class="ot">=</span><span class="st">"daniel"</span><span class="kw">></span></span> <span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a> <span class="kw"><input</span> <span class="er">type</span><span class="ot">=</span><span class="st">submit</span> <span class="er">name</span><span class="ot">=</span><span class="st">"press"</span> <span class="er">value</span><span class="ot">=</span><span class="st">"OK"</span><span class="kw">></span></span> <span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a><span class="kw"></form></span></span></code></pre></div> <p>To POST this with curl, you do not have to think about if the fields are hidden or not. To curl they are all the same:</p> <pre><code>curl --data "birthyear=1905&press=OK&person=daniel" [URL]</code></pre> <h2 id="figure-out-what-a-post-looks-like">Figure Out What A POST Looks Like</h2> <p>When you are about to fill in a form and send it to a server by using curl instead of a browser, you are of course interested in sending a POST exactly the way your browser does.</p> <p>An easy way to get to see this, is to save the HTML page with the form on your local disk, modify the 'method' to a GET, and press the submit button (you could also change the action URL if you want to).</p> <p>You then clearly see the data get appended to the URL, separated with a <code>?</code>-letter as GET forms are supposed to.</p> <h1 id="http-upload">HTTP upload</h1> <h2 id="put">PUT</h2> <p>Perhaps the best way to upload data to an HTTP server is to use PUT. Then again, this of course requires that someone put a program or script on the server end that knows how to receive an HTTP PUT stream.</p> <p>Put a file to an HTTP server with curl:</p> <pre><code>curl --upload-file uploadfile http://www.example.com/receive.cgi</code></pre> <h1 id="http-authentication">HTTP Authentication</h1> <h2 id="basic-authentication">Basic Authentication</h2> <p>HTTP Authentication is the ability to tell the server your username and password so that it can verify that you are allowed to do the request you are doing. The Basic authentication used in HTTP (which is the type curl uses by default) is <strong>plain text</strong> based, which means it sends username and password only slightly obfuscated, but still fully readable by anyone that sniffs on the network between you and the remote server.</p> <p>To tell curl to use a user and password for authentication:</p> <pre><code>curl --user name:password http://www.example.com</code></pre> <h2 id="other-authentication">Other Authentication</h2> <p>The site might require a different authentication method (check the headers returned by the server), and then <a href="https://curl.se/docs/manpage.html#--ntlm"><code>--ntlm</code></a>, <a href="https://curl.se/docs/manpage.html#--digest"><code>--digest</code></a>, <a href="https://curl.se/docs/manpage.html#--negotiate"><code>--negotiate</code></a> or even <a href="https://curl.se/docs/manpage.html#--anyauth"><code>--anyauth</code></a> might be options that suit you.</p> <h2 id="proxy-authentication">Proxy Authentication</h2> <p>Sometimes your HTTP access is only available through the use of an HTTP proxy. This seems to be especially common at various companies. An HTTP proxy may require its own user and password to allow the client to get through to the Internet. To specify those with curl, run something like:</p> <pre><code>curl --proxy-user proxyuser:proxypassword curl.se</code></pre> <p>If your proxy requires the authentication to be done using the NTLM method, use <a href="https://curl.se/docs/manpage.html#--proxy-ntlm"><code>--proxy-ntlm</code></a>, if it requires Digest use <a href="https://curl.se/docs/manpage.html#--proxy-digest"><code>--proxy-digest</code></a>.</p> <p>If you use any one of these user+password options but leave out the password part, curl prompts for the password interactively.</p> <h2 id="hiding-credentials">Hiding credentials</h2> <p>Do note that when a program is run, its parameters might be possible to see when listing the running processes of the system. Thus, other users may be able to watch your passwords if you pass them as plain command line options. There are ways to circumvent this.</p> <p>It is worth noting that while this is how HTTP Authentication works, many websites do not use this concept when they provide logins etc. See the Web Login chapter further below for more details on that.</p> <h1 id="more-http-headers">More HTTP Headers</h1> <h2 id="referer">Referer</h2> <p>An HTTP request may include a 'referer' field (yes it is misspelled), which can be used to tell from which URL the client got to this particular resource. Some programs/scripts check the referer field of requests to verify that this was not arriving from an external site or an unknown page. While this is a stupid way to check something so easily forged, many scripts still do it. Using curl, you can put anything you want in the referer-field and thus more easily be able to fool the server into serving your request.</p> <p>Use curl to set the referer field with:</p> <pre><code>curl --referer http://www.example.come http://www.example.com</code></pre> <h2 id="user-agent">User Agent</h2> <p>Similar to the referer field, all HTTP requests may set the User-Agent field. It names what user agent (client) that is being used. Many applications use this information to decide how to display pages. Silly web programmers try to make different pages for users of different browsers to make them look the best possible for their particular browsers. They usually also do different kinds of JavaScript etc.</p> <p>At times, you may learn that getting a page with curl does not return the same page that you see when getting the page with your browser. Then you know it is time to set the User Agent field to fool the server into thinking you are one of those browsers.</p> <p>By default, curl uses curl/VERSION, such as User-Agent: curl/8.11.0.</p> <p>To make curl look like Internet Explorer 5 on a Windows 2000 box:</p> <pre><code>curl --user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" [URL]</code></pre> <p>Or why not look like you are using Netscape 4.73 on an old Linux box:</p> <pre><code>curl --user-agent "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" [URL]</code></pre> <h2 id="redirects">Redirects</h2> <h2 id="location-header">Location header</h2> <p>When a resource is requested from a server, the reply from the server may include a hint about where the browser should go next to find this page, or a new page keeping newly generated output. The header that tells the browser to redirect is <code>Location:</code>.</p> <p>Curl does not follow <code>Location:</code> headers by default, but simply displays such pages in the same manner it displays all HTTP replies. It does however feature an option that makes it attempt to follow the <code>Location:</code> pointers.</p> <p>To tell curl to follow a Location:</p> <pre><code>curl --location http://www.example.com</code></pre> <p>If you use curl to POST to a site that immediately redirects you to another page, you can safely use <a href="https://curl.se/docs/manpage.html#-L"><code>--location</code></a> (<code>-L</code>) and <code>--data</code>/<code>--form</code> together. Curl only uses POST in the first request, and then revert to GET in the following operations.</p> <h2 id="other-redirects">Other redirects</h2> <p>Browsers typically support at least two other ways of redirects that curl does not: first the html may contain a meta refresh tag that asks the browser to load a specific URL after a set number of seconds, or it may use JavaScript to do it.</p> <h1 id="cookies">Cookies</h1> <h2 id="cookie-basics">Cookie Basics</h2> <p>The way the web browsers do "client side state control" is by using cookies. Cookies are just names with associated contents. The cookies are sent to the client by the server. The server tells the client for what path and hostname it wants the cookie sent back, and it also sends an expiration date and a few more properties.</p> <p>When a client communicates with a server with a name and path as previously specified in a received cookie, the client sends back the cookies and their contents to the server, unless of course they are expired.</p> <p>Many applications and servers use this method to connect a series of requests into a single logical session. To be able to use curl in such occasions, we must be able to record and send back cookies the way the web application expects them. The same way browsers deal with them.</p> <h2 id="cookie-options">Cookie options</h2> <p>The simplest way to send a few cookies to the server when getting a page with curl is to add them on the command line like:</p> <pre><code>curl --cookie "name=Daniel" http://www.example.com</code></pre> <p>Cookies are sent as common HTTP headers. This is practical as it allows curl to record cookies simply by recording headers. Record cookies with curl by using the <a href="https://curl.se/docs/manpage.html#-D"><code>--dump-header</code></a> (<code>-D</code>) option like:</p> <pre><code>curl --dump-header headers_and_cookies http://www.example.com</code></pre> <p>(Take note that the <a href="https://curl.se/docs/manpage.html#-c"><code>--cookie-jar</code></a> option described below is a better way to store cookies.)</p> <p>Curl has a full blown cookie parsing engine built-in that comes in use if you want to reconnect to a server and use cookies that were stored from a previous connection (or hand-crafted manually to fool the server into believing you had a previous connection). To use previously stored cookies, you run curl like:</p> <pre><code>curl --cookie stored_cookies_in_file http://www.example.com</code></pre> <p>Curl's "cookie engine" gets enabled when you use the <a href="https://curl.se/docs/manpage.html#-b"><code>--cookie</code></a> option. If you only want curl to understand received cookies, use <code>--cookie</code> with a file that does not exist. Example, if you want to let curl understand cookies from a page and follow a location (and thus possibly send back cookies it received), you can invoke it like:</p> <pre><code>curl --cookie nada --location http://www.example.com</code></pre> <p>Curl has the ability to read and write cookie files that use the same file format that Netscape and Mozilla once used. It is a convenient way to share cookies between scripts or invokes. The <code>--cookie</code> (<code>-b</code>) switch automatically detects if a given file is such a cookie file and parses it, and by using the <code>--cookie-jar</code> (<code>-c</code>) option you make curl write a new cookie file at the end of an operation:</p> <pre><code>curl --cookie cookies.txt --cookie-jar newcookies.txt http://www.example.com</code></pre> <h1 id="https">HTTPS</h1> <h2 id="https-is-http-secure">HTTPS is HTTP secure</h2> <p>There are a few ways to do secure HTTP transfers. By far the most common protocol for doing this is what is generally known as HTTPS, HTTP over SSL. SSL encrypts all the data that is sent and received over the network and thus makes it harder for attackers to spy on sensitive information.</p> <p>SSL (or TLS as the current version of the standard is called) offers a set of advanced features to do secure transfers over HTTP.</p> <p>Curl supports encrypted fetches when built to use a TLS library and it can be built to use one out of a fairly large set of libraries - <code>curl -V</code> shows which one your curl was built to use (if any). To get a page from an HTTPS server, simply run curl like:</p> <pre><code>curl https://secure.example.com</code></pre> <h2 id="certificates">Certificates</h2> <p>In the HTTPS world, you use certificates to validate that you are the one you claim to be, as an addition to normal passwords. Curl supports client- side certificates. All certificates are locked with a passphrase, which you need to enter before the certificate can be used by curl. The passphrase can be specified on the command line or if not, entered interactively when curl queries for it. Use a certificate with curl on an HTTPS server like:</p> <pre><code>curl --cert mycert.pem https://secure.example.com</code></pre> <p>curl also tries to verify that the server is who it claims to be, by verifying the server's certificate against a locally stored CA cert bundle. Failing the verification causes curl to deny the connection. You must then use <a href="https://curl.se/docs/manpage.html#-k"><code>--insecure</code></a> (<code>-k</code>) in case you want to tell curl to ignore that the server cannot be verified.</p> <p>More about server certificate verification and ca cert bundles can be read in the <a href="https://curl.se/docs/sslcerts.html"><code>SSLCERTS</code> document</a>.</p> <p>At times you may end up with your own CA cert store and then you can tell curl to use that to verify the server's certificate:</p> <pre><code>curl --cacert ca-bundle.pem https://example.com/</code></pre> <h1 id="custom-request-elements">Custom Request Elements</h1> <h2 id="modify-method-and-headers">Modify method and headers</h2> <p>Doing fancy stuff, you may need to add or change elements of a single curl request.</p> <p>For example, you can change the POST method to <code>PROPFIND</code> and send the data as <code>Content-Type: text/xml</code> (instead of the default <code>Content-Type</code>) like this:</p> <pre><code>curl --data "<xml>" --header "Content-Type: text/xml" --request PROPFIND example.com</code></pre> <p>You can delete a default header by providing one without content. Like you can ruin the request by chopping off the <code>Host:</code> header:</p> <pre><code>curl --header "Host:" http://www.example.com</code></pre> <p>You can add headers the same way. Your server may want a <code>Destination:</code> header, and you can add it:</p> <pre><code>curl --header "Destination: http://nowhere" http://example.com</code></pre> <h2 id="more-on-changed-methods">More on changed methods</h2> <p>It should be noted that curl selects which methods to use on its own depending on what action to ask for. <code>-d</code> makes a POST, <code>-I</code> makes a HEAD and so on. If you use the <a href="https://curl.se/docs/manpage.html#-X"><code>--request</code></a> / <code>-X</code> option you can change the method keyword curl selects, but you do not modify curl's behavior. This means that if you for example use -d "data" to do a POST, you can modify the method to a <code>PROPFIND</code> with <code>-X</code> and curl still thinks it sends a POST. You can change the normal GET to a POST method by simply adding <code>-X POST</code> in a command line like:</p> <pre><code>curl -X POST http://example.org/</code></pre> <p>curl however still acts as if it sent a GET so it does not send any request body etc.</p> <h1 id="web-login">Web Login</h1> <h2 id="some-login-tricks">Some login tricks</h2> <p>While not strictly just HTTP related, it still causes a lot of people problems so here's the executive run-down of how the vast majority of all login forms work and how to login to them using curl.</p> <p>It can also be noted that to do this properly in an automated fashion, you most certainly need to script things and do multiple curl invokes etc.</p> <p>First, servers mostly use cookies to track the logged-in status of the client, so you need to capture the cookies you receive in the responses. Then, many sites also set a special cookie on the login page (to make sure you got there through their login page) so you should make a habit of first getting the login-form page to capture the cookies set there.</p> <p>Some web-based login systems feature various amounts of JavaScript, and sometimes they use such code to set or modify cookie contents. Possibly they do that to prevent programmed logins, like this manual describes how to... Anyway, if reading the code is not enough to let you repeat the behavior manually, capturing the HTTP requests done by your browsers and analyzing the sent cookies is usually a working method to work out how to shortcut the JavaScript need.</p> <p>In the actual <code><form></code> tag for the login, lots of sites fill-in random/session or otherwise secretly generated hidden tags and you may need to first capture the HTML code for the login form and extract all the hidden fields to be able to do a proper login POST. Remember that the contents need to be URL encoded when sent in a normal POST.</p> <h1 id="debug">Debug</h1> <h2 id="some-debug-tricks">Some debug tricks</h2> <p>Many times when you run curl on a site, you notice that the site does not seem to respond the same way to your curl requests as it does to your browser's.</p> <p>Then you need to start making your curl requests more similar to your browser's requests:</p> <ul> <li><p>Use the <code>--trace-ascii</code> option to store fully detailed logs of the requests for easier analyzing and better understanding</p></li> <li><p>Make sure you check for and use cookies when needed (both reading with <code>--cookie</code> and writing with <code>--cookie-jar</code>)</p></li> <li><p>Set user-agent (with <a href="https://curl.se/docs/manpage.html#-A"><code>-A</code></a>) to one like a recent popular browser does</p></li> <li><p>Set referer (with <a href="https://curl.se/docs/manpage.html#-E"><code>-E</code></a>) like it is set by the browser</p></li> <li><p>If you use POST, make sure you send all the fields and in the same order as the browser does it.</p></li> </ul> <h2 id="check-what-the-browsers-do">Check what the browsers do</h2> <p>A good helper to make sure you do this right, is the web browsers' developers tools that let you view all headers you send and receive (even when using HTTPS).</p> <p>A more raw approach is to capture the HTTP traffic on the network with tools such as Wireshark or tcpdump and check what headers that were sent and received by the browser. (HTTPS forces you to use <code>SSLKEYLOGFILE</code> to do that.)</p> </div> </div> </body> </html>