TheArtOfHttpScripting 18 KB

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