TheArtOfHttpScripting 16 KB

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