1 Online: http://curl.haxx.se/docs/httpscripting.shtml
4 The Art Of Scripting HTTP Requests Using Curl
5 =============================================
7 This document will assume that you're familiar with HTML and general
10 The possibility to write scripts is essential to make a good computer
11 system. Unix' capability to be extended by shell scripts and various tools to
12 run various automated commands and scripts is one reason why it has succeeded
15 The increasing amount of applications moving to the web has made "HTTP
16 Scripting" more frequently requested and wanted. To be able to automatically
17 extract information from the web, to fake users, to post or upload data to
18 web servers are all important tasks today.
20 Curl is a command line tool for doing all sorts of URL manipulations and
21 transfers, but this particular document will focus on how to use it when
22 doing HTTP requests for fun and profit. I'll assume that you know how to
23 invoke 'curl --help' or 'curl --manual' to get basic information about it.
25 Curl is not written to do everything for you. It makes the requests, it gets
26 the data, it sends data and it retrieves the information. You probably need
27 to glue everything together using some kind of script language or repeated
32 HTTP is the protocol used to fetch data from web servers. It is a very simple
33 protocol that is built upon TCP/IP. The protocol also allows information to
34 get sent to the server from the client using a few different methods, as will
37 HTTP is plain ASCII text lines being sent by the client to a server to
38 request a particular action, and then the server replies a few text lines
39 before the actual requested content is sent to the client.
41 Using curl's option -v will display what kind of commands curl sends to the
42 server, as well as a few other informational texts. -v is the single most
43 useful option when it comes to debug or even understand the curl<->server
48 The Uniform Resource Locator format is how you specify the address of a
49 particular resource on the Internet. You know these, you've seen URLs like
50 http://curl.haxx.se or https://yourbank.com a million times.
54 The simplest and most common request/operation made using HTTP is to get a
55 URL. The URL could itself refer to a web page, an image or a file. The client
56 issues a GET request to the server and receives the document it asked for.
57 If you issue the command line
59 curl http://curl.haxx.se
61 you get a web page returned in your terminal window. The entire HTML document
64 All HTTP replies contain a set of headers that are normally hidden, use
65 curl's -i option to display them as well as the rest of the document. You can
66 also ask the remote server for ONLY the headers by using the -I option (which
67 will make curl issue a HEAD request).
71 Forms are the general way a web site can present a HTML page with fields for
72 the user to enter data in, and then press some kind of 'OK' or 'submit'
73 button to get that data sent to the server. The server then typically uses
74 the posted data to decide how to act. Like using the entered words to search
75 in a database, or to add the info in a bug track system, display the entered
76 address on a map or using the info as a login-prompt verifying that the user
77 is allowed to see what it is about to see.
79 Of course there has to be some kind of program in the server end to receive
80 the data you send. You cannot just invent something out of the air.
84 A GET-form uses the method GET, as specified in HTML like:
86 <form method="GET" action="junk.cgi">
87 <input type=text name="birthyear">
88 <input type=submit name=press value="OK">
91 In your favorite browser, this form will appear with a text box to fill in
92 and a press-button labeled "OK". If you fill in '1905' and press the OK
93 button, your browser will then create a new URL to get for you. The URL will
94 get "junk.cgi?birthyear=1905&press=OK" appended to the path part of the
97 If the original form was seen on the page "www.hotmail.com/when/birth.html",
98 the second page you'll get will become
99 "www.hotmail.com/when/junk.cgi?birthyear=1905&press=OK".
101 Most search engines work this way.
103 To make curl do the GET form post for you, just enter the expected created
106 curl "www.hotmail.com/when/junk.cgi?birthyear=1905&press=OK"
110 The GET method makes all input field names get displayed in the URL field of
111 your browser. That's generally a good thing when you want to be able to
112 bookmark that page with your given data, but it is an obvious disadvantage
113 if you entered secret information in one of the fields or if there are a
114 large amount of fields creating a very long and unreadable URL.
116 The HTTP protocol then offers the POST method. This way the client sends the
117 data separated from the URL and thus you won't see any of it in the URL
120 The form would look very similar to the previous one:
122 <form method="POST" action="junk.cgi">
123 <input type=text name="birthyear">
124 <input type=submit name=press value=" OK ">
127 And to use curl to post this form with the same data filled in as before, we
130 curl -d "birthyear=1905&press=%20OK%20" www.hotmail.com/when/junk.cgi
132 This kind of POST will use the Content-Type
133 application/x-www-form-urlencoded and is the most widely used POST kind.
135 The data you send to the server MUST already be properly encoded, curl will
136 not do that for you. For example, if you want the data to contain a space,
137 you need to replace that space with %20 etc. Failing to comply with this
138 will most likely cause your data to be received wrongly and messed up.
142 Back in late 1995 they defined an additional way to post data over HTTP. It
143 is documented in the RFC 1867, why this method sometimes is referred to as
146 This method is mainly designed to better support file uploads. A form that
147 allows a user to upload a file could be written like this in HTML:
149 <form method="POST" enctype='multipart/form-data' action="upload.cgi">
150 <input type=file name=upload>
151 <input type=submit name=press value="OK">
154 This clearly shows that the Content-Type about to be sent is
157 To post to a form like this with curl, you enter a command line like:
159 curl -F upload=@localfilename -F press=OK [URL]
163 A very common way for HTML based application to pass state information
164 between pages is to add hidden fields to the forms. Hidden fields are
165 already filled in, they aren't displayed to the user and they get passed
166 along just as all the other fields.
168 A similar example form with one visible field, one hidden field and one
169 submit button could look like:
171 <form method="POST" action="foobar.cgi">
172 <input type=text name="birthyear">
173 <input type=hidden name="person" value="daniel">
174 <input type=submit name="press" value="OK">
177 To post this with curl, you won't have to think about if the fields are
178 hidden or not. To curl they're all the same:
180 curl -d "birthyear=1905&press=OK&person=daniel" [URL]
182 4.5 Figure Out What A POST Looks Like
184 When you're about fill in a form and send to a server by using curl instead
185 of a browser, you're of course very interested in sending a POST exactly the
186 way your browser does.
188 An easy way to get to see this, is to save the HTML page with the form on
189 your local disk, modify the 'method' to a GET, and press the submit button
190 (you could also change the action URL if you want to).
192 You will then clearly see the data get appended to the URL, separated with a
193 '?'-letter as GET forms are supposed to.
197 The perhaps best way to upload data to a HTTP server is to use PUT. Then
198 again, this of course requires that someone put a program or script on the
199 server end that knows how to receive a HTTP PUT stream.
201 Put a file to a HTTP server with curl:
203 curl -T uploadfile www.uploadhttp.com/receive.cgi
207 Authentication is the ability to tell the server your username and password
208 so that it can verify that you're allowed to do the request you're doing. The
209 Basic authentication used in HTTP (which is the type curl uses by default) is
210 *plain* *text* based, which means it sends username and password only
211 slightly obfuscated, but still fully readable by anyone that sniffs on the
212 network between you and the remote server.
214 To tell curl to use a user and password for authentication:
216 curl -u name:password www.secrets.com
218 The site might require a different authentication method (check the headers
219 returned by the server), and then --ntlm, --digest, --negotiate or even
220 --anyauth might be options that suit you.
222 Sometimes your HTTP access is only available through the use of a HTTP
223 proxy. This seems to be especially common at various companies. A HTTP proxy
224 may require its own user and password to allow the client to get through to
225 the Internet. To specify those with curl, run something like:
227 curl -U proxyuser:proxypassword curl.haxx.se
229 If your proxy requires the authentication to be done using the NTLM method,
230 use --proxy-ntlm, if it requires Digest use --proxy-digest.
232 If you use any one these user+password options but leave out the password
233 part, curl will prompt for the password interactively.
235 Do note that when a program is run, its parameters might be possible to see
236 when listing the running processes of the system. Thus, other users may be
237 able to watch your passwords if you pass them as plain command line
238 options. There are ways to circumvent this.
242 A HTTP request may include a 'referer' field (yes it is misspelled), which
243 can be used to tell from which URL the client got to this particular
244 resource. Some programs/scripts check the referer field of requests to verify
245 that this wasn't arriving from an external site or an unknown page. While
246 this is a stupid way to check something so easily forged, many scripts still
247 do it. Using curl, you can put anything you want in the referer-field and
248 thus more easily be able to fool the server into serving your request.
250 Use curl to set the referer field with:
252 curl -e http://curl.haxx.se daniel.haxx.se
256 Very similar to the referer field, all HTTP requests may set the User-Agent
257 field. It names what user agent (client) that is being used. Many
258 applications use this information to decide how to display pages. Silly web
259 programmers try to make different pages for users of different browsers to
260 make them look the best possible for their particular browsers. They usually
261 also do different kinds of javascript, vbscript etc.
263 At times, you will see that getting a page with curl will not return the same
264 page that you see when getting the page with your browser. Then you know it
265 is time to set the User Agent field to fool the server into thinking you're
266 one of those browsers.
268 To make curl look like Internet Explorer on a Windows 2000 box:
270 curl -A "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" [URL]
272 Or why not look like you're using Netscape 4.73 on a Linux (PIII) box:
274 curl -A "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" [URL]
278 When a resource is requested from a server, the reply from the server may
279 include a hint about where the browser should go next to find this page, or a
280 new page keeping newly generated output. The header that tells the browser
281 to redirect is Location:.
283 Curl does not follow Location: headers by default, but will simply display
284 such pages in the same manner it display all HTTP replies. It does however
285 feature an option that will make it attempt to follow the Location: pointers.
287 To tell curl to follow a Location:
289 curl -L www.sitethatredirects.com
291 If you use curl to POST to a site that immediately redirects you to another
292 page, you can safely use -L and -d/-F together. Curl will only use POST in
293 the first request, and then revert to GET in the following operations.
297 The way the web browsers do "client side state control" is by using
298 cookies. Cookies are just names with associated contents. The cookies are
299 sent to the client by the server. The server tells the client for what path
300 and host name it wants the cookie sent back, and it also sends an expiration
301 date and a few more properties.
303 When a client communicates with a server with a name and path as previously
304 specified in a received cookie, the client sends back the cookies and their
305 contents to the server, unless of course they are expired.
307 Many applications and servers use this method to connect a series of requests
308 into a single logical session. To be able to use curl in such occasions, we
309 must be able to record and send back cookies the way the web application
310 expects them. The same way browsers deal with them.
312 The simplest way to send a few cookies to the server when getting a page with
313 curl is to add them on the command line like:
315 curl -b "name=Daniel" www.cookiesite.com
317 Cookies are sent as common HTTP headers. This is practical as it allows curl
318 to record cookies simply by recording headers. Record cookies with curl by
319 using the -D option like:
321 curl -D headers_and_cookies www.cookiesite.com
323 (Take note that the -c option described below is a better way to store
326 Curl has a full blown cookie parsing engine built-in that comes to use if you
327 want to reconnect to a server and use cookies that were stored from a
328 previous connection (or handicrafted manually to fool the server into
329 believing you had a previous connection). To use previously stored cookies,
332 curl -b stored_cookies_in_file www.cookiesite.com
334 Curl's "cookie engine" gets enabled when you use the -b option. If you only
335 want curl to understand received cookies, use -b with a file that doesn't
336 exist. Example, if you want to let curl understand cookies from a page and
337 follow a location (and thus possibly send back cookies it received), you can
340 curl -b nada -L www.cookiesite.com
342 Curl has the ability to read and write cookie files that use the same file
343 format that Netscape and Mozilla do. It is a convenient way to share cookies
344 between browsers and automatic scripts. The -b switch automatically detects
345 if a given file is such a cookie file and parses it, and by using the
346 -c/--cookie-jar option you'll make curl write a new cookie file at the end of
349 curl -b cookies.txt -c newcookies.txt www.cookiesite.com
353 There are a few ways to do secure HTTP transfers. The by far most common
354 protocol for doing this is what is generally known as HTTPS, HTTP over
355 SSL. SSL encrypts all the data that is sent and received over the network and
356 thus makes it harder for attackers to spy on sensitive information.
358 SSL (or TLS as the latest version of the standard is called) offers a
359 truckload of advanced features to allow all those encryptions and key
360 infrastructure mechanisms encrypted HTTP requires.
362 Curl supports encrypted fetches thanks to the freely available OpenSSL
363 libraries. To get a page from a HTTPS server, simply run curl like:
365 curl https://that.secure.server.com
369 In the HTTPS world, you use certificates to validate that you are the one
370 you you claim to be, as an addition to normal passwords. Curl supports
371 client-side certificates. All certificates are locked with a pass phrase,
372 which you need to enter before the certificate can be used by curl. The pass
373 phrase can be specified on the command line or if not, entered interactively
374 when curl queries for it. Use a certificate with curl on a HTTPS server
377 curl -E mycert.pem https://that.secure.server.com
379 curl also tries to verify that the server is who it claims to be, by
380 verifying the server's certificate against a locally stored CA cert
381 bundle. Failing the verification will cause curl to deny the connection. You
382 must then use -k in case you want to tell curl to ignore that the server
385 More about server certificate verification and ca cert bundles can be read
386 in the SSLCERTS document, available online here:
388 http://curl.haxx.se/docs/sslcerts.html
390 12. Custom Request Elements
392 Doing fancy stuff, you may need to add or change elements of a single curl
395 For example, you can change the POST request to a PROPFIND and send the data
396 as "Content-Type: text/xml" (instead of the default Content-Type) like this:
398 curl -d "<xml>" -H "Content-Type: text/xml" -X PROPFIND url.com
400 You can delete a default header by providing one without content. Like you
401 can ruin the request by chopping off the Host: header:
403 curl -H "Host:" http://mysite.com
405 You can add headers the same way. Your server may want a "Destination:"
406 header, and you can add it:
408 curl -H "Destination: http://moo.com/nowhere" http://url.com
412 Many times when you run curl on a site, you'll notice that the site doesn't
413 seem to respond the same way to your curl requests as it does to your
416 Then you need to start making your curl requests more similar to your
419 * Use the --trace-ascii option to store fully detailed logs of the requests
420 for easier analyzing and better understanding
422 * Make sure you check for and use cookies when needed (both reading with -b
425 * Set user-agent to one like a recent popular browser does
427 * Set referer like it is set by the browser
429 * If you use POST, make sure you send all the fields and in the same order as
430 the browser does it. (See chapter 4.5 above)
432 A very good helper to make sure you do this right, is the LiveHTTPHeader tool
433 that lets you view all headers you send and receive with Mozilla/Firefox
434 (even when using HTTPS).
436 A more raw approach is to capture the HTTP traffic on the network with tools
437 such as ethereal or tcpdump and check what headers that were sent and
438 received by the browser. (HTTPS makes this technique inefficient.)
442 RFC 2616 is a must to read if you want in-depth understanding of the HTTP
445 RFC 2396 explains the URL syntax.
447 RFC 2109 defines how cookies are supposed to work.
449 RFC 1867 defines the HTTP post upload format.
451 http://www.openssl.org is the home of the OpenSSL project
453 http://curl.haxx.se is the home of the cURL project