TheArtOfHttpScripting 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. Updated: Dec 24, 2013 (http://curl.haxx.se/docs/httpscripting.html)
  2. _ _ ____ _
  3. ___| | | | _ \| |
  4. / __| | | | |_) | |
  5. | (__| |_| | _ <| |___
  6. \___|\___/|_| \_\_____|
  7. The Art Of Scripting HTTP Requests Using Curl
  8. 1. HTTP Scripting
  9. 1.1 Background
  10. 1.2 The HTTP Protocol
  11. 1.3 See the Protocol
  12. 1.4 See the Timing
  13. 1.5 See the Response
  14. 2. URL
  15. 2.1 Spec
  16. 2.2 Host
  17. 2.3 Port number
  18. 2.4 User name and password
  19. 2.5 Path part
  20. 3. Fetch a page
  21. 3.1 GET
  22. 3.2 HEAD
  23. 4. HTML forms
  24. 4.1 Forms explained
  25. 4.2 GET
  26. 4.3 POST
  27. 4.4 File Upload POST
  28. 4.5 Hidden Fields
  29. 4.6 Figure Out What A POST Looks Like
  30. 5. HTTP upload
  31. 5.1 PUT
  32. 6. HTTP Authentication
  33. 6.1 Basic Authentication
  34. 6.2 Other Authentication
  35. 6.3 Proxy Authentication
  36. 6.4 Hiding credentials
  37. 7. More HTTP Headers
  38. 7.1 Referer
  39. 7.2 User Agent
  40. 8. Redirects
  41. 8.1 Location header
  42. 8.2 Other redirects
  43. 9. Cookies
  44. 9.1 Cookie Basics
  45. 9.2 Cookie options
  46. 10. HTTPS
  47. 10.1 HTTPS is HTTP secure
  48. 10.2 Certificates
  49. 11. Custom Request Elements
  50. 11.1 Modify method and headers
  51. 11.2 More on changed methods
  52. 12. Web Login
  53. 12.1 Some login tricks
  54. 13. Debug
  55. 13.1 Some debug tricks
  56. 14. References
  57. 14.1 Standards
  58. 14.2 Sites
  59. ==============================================================================
  60. 1. HTTP Scripting
  61. 1.1 Background
  62. This document assumes that you're familiar with HTML and general networking.
  63. The increasing amount of applications moving to the web has made "HTTP
  64. Scripting" more frequently requested and wanted. To be able to automatically
  65. extract information from the web, to fake users, to post or upload data to
  66. web servers are all important tasks today.
  67. Curl is a command line tool for doing all sorts of URL manipulations and
  68. transfers, but this particular document will focus on how to use it when
  69. doing HTTP requests for fun and profit. I'll assume that you know how to
  70. invoke 'curl --help' or 'curl --manual' to get basic information about it.
  71. Curl is not written to do everything for you. It makes the requests, it gets
  72. the data, it sends data and it retrieves the information. You probably need
  73. to glue everything together using some kind of script language or repeated
  74. manual invokes.
  75. 1.2 The HTTP Protocol
  76. HTTP is the protocol used to fetch data from web servers. It is a very simple
  77. protocol that is built upon TCP/IP. The protocol also allows information to
  78. get sent to the server from the client using a few different methods, as will
  79. be shown here.
  80. HTTP is plain ASCII text lines being sent by the client to a server to
  81. request a particular action, and then the server replies a few text lines
  82. before the actual requested content is sent to the client.
  83. The client, curl, sends a HTTP request. The request contains a method (like
  84. GET, POST, HEAD etc), a number of request headers and sometimes a request
  85. body. The HTTP server responds with a status line (indicating if things went
  86. well), response headers and most often also a response body. The "body" part
  87. is the plain data you requested, like the actual HTML or the image etc.
  88. 1.3 See the Protocol
  89. Using curl's option --verbose (-v as a short option) will display what kind
  90. of commands curl sends to the server, as well as a few other informational
  91. texts.
  92. --verbose is the single most useful option when it comes to debug or even
  93. understand the curl<->server interaction.
  94. Sometimes even --verbose is not enough. Then --trace and --trace-ascii offer
  95. even more details as they show EVERYTHING curl sends and receives. Use it
  96. like this:
  97. curl --trace-ascii debugdump.txt http://www.example.com/
  98. 1.4 See the Timing
  99. Many times you may wonder what exactly is taking all the time, or you just
  100. want to know the amount of milliseconds between two points in a
  101. transfer. For those, and other similar situations, the --trace-time option
  102. is what you need. It'll prepend the time to each trace output line:
  103. curl --trace-ascii d.txt --trace-time http://example.com/
  104. 1.5 See the Response
  105. By default curl sends the response to stdout. You need to redirect it
  106. somewhere to avoid that, most often that is done with -o or -O.
  107. 2. URL
  108. 2.1 Spec
  109. The Uniform Resource Locator format is how you specify the address of a
  110. particular resource on the Internet. You know these, you've seen URLs like
  111. http://curl.haxx.se or https://yourbank.com a million times. RFC 3986 is the
  112. canonical spec.
  113. 2.2 Host
  114. The host name is usually resolved using DNS or your /etc/hosts file to an IP
  115. address and that's what curl will communicate with. Alternatively you specify
  116. the IP address directly in the URL instead of a name.
  117. For development and other trying out situation, you can point out a different
  118. IP address for a host name than what would otherwise be used, by using curl's
  119. --resolve option:
  120. curl --resolve www.example.org:80:127.0.0.1 http://www.example.org/
  121. 2.3 Port number
  122. Each protocol curl supports operate on a default port number, be it over TCP
  123. or in some cases UDP. Normally you don't have to take that into
  124. consideration, but at times you run test servers on other ports or
  125. similar. Then you can specify the port number in the URL with a colon and a
  126. number immediately following the host name. Like when doing HTTP to port
  127. 1234:
  128. curl http://www.example.org:1234/
  129. The port number you specify in the URL is the number that the server uses to
  130. offer its services. Sometimes you may use a local proxy, and then you may
  131. need to specify that proxy's port number separate on what curl needs to
  132. connect to locally. Like when using a HTTP proxy on port 4321:
  133. curl --proxy http://proxy.example.org:4321 http://remote.example.org/
  134. 2.4 User name and password
  135. Some services are setup to require HTTP authentication and then you need to
  136. provide name and password which then is transferred to the remote site in
  137. various ways depending on the exact authentication protocol used.
  138. You can opt to either insert the user and password in the URL or you can
  139. provide them separately:
  140. curl http://user:password@example.org/
  141. or
  142. curl -u user:password http://example.org/
  143. You need to pay attention that this kind of HTTP authentication is not what
  144. is usually done and requested by user-oriented web sites these days. They
  145. tend to use forms and cookies instead.
  146. 2.5 Path part
  147. The path part is just sent off to the server to request that it sends back
  148. the associated response. The path is what is to the right side of the slash
  149. that follows the host name and possibly port number.
  150. 3. Fetch a page
  151. 3.1 GET
  152. The simplest and most common request/operation made using HTTP is to get a
  153. URL. The URL could itself refer to a web page, an image or a file. The client
  154. issues a GET request to the server and receives the document it asked for.
  155. If you issue the command line
  156. curl http://curl.haxx.se
  157. you get a web page returned in your terminal window. The entire HTML document
  158. that that URL holds.
  159. All HTTP replies contain a set of response headers that are normally hidden,
  160. use curl's --include (-i) option to display them as well as the rest of the
  161. document.
  162. 3.2 HEAD
  163. You can ask the remote server for ONLY the headers by using the --head (-I)
  164. option which will make curl issue a HEAD request. In some special cases
  165. servers deny the HEAD method while others still work, which is a particular
  166. kind of annoyance.
  167. The HEAD method is defined and made so that the server returns the headers
  168. exactly the way it would do for a GET, but without a body. It means that you
  169. may see a Content-Length: in the response headers, but there must not be an
  170. actual body in the HEAD response.
  171. 4. HTML forms
  172. 4.1 Forms explained
  173. Forms are the general way a web site can present a HTML page with fields for
  174. the user to enter data in, and then press some kind of 'OK' or 'submit'
  175. button to get that data sent to the server. The server then typically uses
  176. the posted data to decide how to act. Like using the entered words to search
  177. in a database, or to add the info in a bug track system, display the entered
  178. address on a map or using the info as a login-prompt verifying that the user
  179. is allowed to see what it is about to see.
  180. Of course there has to be some kind of program in the server end to receive
  181. the data you send. You cannot just invent something out of the air.
  182. 4.2 GET
  183. A GET-form uses the method GET, as specified in HTML like:
  184. <form method="GET" action="junk.cgi">
  185. <input type=text name="birthyear">
  186. <input type=submit name=press value="OK">
  187. </form>
  188. In your favorite browser, this form will appear with a text box to fill in
  189. and a press-button labeled "OK". If you fill in '1905' and press the OK
  190. button, your browser will then create a new URL to get for you. The URL will
  191. get "junk.cgi?birthyear=1905&press=OK" appended to the path part of the
  192. previous URL.
  193. If the original form was seen on the page "www.hotmail.com/when/birth.html",
  194. the second page you'll get will become
  195. "www.hotmail.com/when/junk.cgi?birthyear=1905&press=OK".
  196. Most search engines work this way.
  197. To make curl do the GET form post for you, just enter the expected created
  198. URL:
  199. curl "http://www.hotmail.com/when/junk.cgi?birthyear=1905&press=OK"
  200. 4.3 POST
  201. The GET method makes all input field names get displayed in the URL field of
  202. your browser. That's generally a good thing when you want to be able to
  203. bookmark that page with your given data, but it is an obvious disadvantage
  204. if you entered secret information in one of the fields or if there are a
  205. large amount of fields creating a very long and unreadable URL.
  206. The HTTP protocol then offers the POST method. This way the client sends the
  207. data separated from the URL and thus you won't see any of it in the URL
  208. address field.
  209. The form would look very similar to the previous one:
  210. <form method="POST" action="junk.cgi">
  211. <input type=text name="birthyear">
  212. <input type=submit name=press value=" OK ">
  213. </form>
  214. And to use curl to post this form with the same data filled in as before, we
  215. could do it like:
  216. curl --data "birthyear=1905&press=%20OK%20" \
  217. http://www.example.com/when.cgi
  218. This kind of POST will use the Content-Type
  219. application/x-www-form-urlencoded and is the most widely used POST kind.
  220. The data you send to the server MUST already be properly encoded, curl will
  221. not do that for you. For example, if you want the data to contain a space,
  222. you need to replace that space with %20 etc. Failing to comply with this
  223. will most likely cause your data to be received wrongly and messed up.
  224. Recent curl versions can in fact url-encode POST data for you, like this:
  225. curl --data-urlencode "name=I am Daniel" http://www.example.com
  226. 4.4 File Upload POST
  227. Back in late 1995 they defined an additional way to post data over HTTP. It
  228. is documented in the RFC 1867, why this method sometimes is referred to as
  229. RFC1867-posting.
  230. This method is mainly designed to better support file uploads. A form that
  231. allows a user to upload a file could be written like this in HTML:
  232. <form method="POST" enctype='multipart/form-data' action="upload.cgi">
  233. <input type=file name=upload>
  234. <input type=submit name=press value="OK">
  235. </form>
  236. This clearly shows that the Content-Type about to be sent is
  237. multipart/form-data.
  238. To post to a form like this with curl, you enter a command line like:
  239. curl --form upload=@localfilename --form press=OK [URL]
  240. 4.5 Hidden Fields
  241. A very common way for HTML based application to pass state information
  242. between pages is to add hidden fields to the forms. Hidden fields are
  243. already filled in, they aren't displayed to the user and they get passed
  244. along just as all the other fields.
  245. A similar example form with one visible field, one hidden field and one
  246. submit button could look like:
  247. <form method="POST" action="foobar.cgi">
  248. <input type=text name="birthyear">
  249. <input type=hidden name="person" value="daniel">
  250. <input type=submit name="press" value="OK">
  251. </form>
  252. To post this with curl, you won't have to think about if the fields are
  253. hidden or not. To curl they're all the same:
  254. curl --data "birthyear=1905&press=OK&person=daniel" [URL]
  255. 4.6 Figure Out What A POST Looks Like
  256. When you're about fill in a form and send to a server by using curl instead
  257. of a browser, you're of course very interested in sending a POST exactly the
  258. way your browser does.
  259. An easy way to get to see this, is to save the HTML page with the form on
  260. your local disk, modify the 'method' to a GET, and press the submit button
  261. (you could also change the action URL if you want to).
  262. You will then clearly see the data get appended to the URL, separated with a
  263. '?'-letter as GET forms are supposed to.
  264. 5. HTTP upload
  265. 5.1 PUT
  266. The perhaps best way to upload data to a HTTP server is to use PUT. Then
  267. again, this of course requires that someone put a program or script on the
  268. server end that knows how to receive a HTTP PUT stream.
  269. Put a file to a HTTP server with curl:
  270. curl --upload-file uploadfile http://www.example.com/receive.cgi
  271. 6. HTTP Authentication
  272. 6.1 Basic Authentication
  273. HTTP Authentication is the ability to tell the server your username and
  274. password so that it can verify that you're allowed to do the request you're
  275. doing. The Basic authentication used in HTTP (which is the type curl uses by
  276. default) is *plain* *text* based, which means it sends username and password
  277. only slightly obfuscated, but still fully readable by anyone that sniffs on
  278. the network between you and the remote server.
  279. To tell curl to use a user and password for authentication:
  280. curl --user name:password http://www.example.com
  281. 6.2 Other Authentication
  282. The site might require a different authentication method (check the headers
  283. returned by the server), and then --ntlm, --digest, --negotiate or even
  284. --anyauth might be options that suit you.
  285. 6.3 Proxy Authentication
  286. Sometimes your HTTP access is only available through the use of a HTTP
  287. proxy. This seems to be especially common at various companies. A HTTP proxy
  288. may require its own user and password to allow the client to get through to
  289. the Internet. To specify those with curl, run something like:
  290. curl --proxy-user proxyuser:proxypassword curl.haxx.se
  291. If your proxy requires the authentication to be done using the NTLM method,
  292. use --proxy-ntlm, if it requires Digest use --proxy-digest.
  293. If you use any one these user+password options but leave out the password
  294. part, curl will prompt for the password interactively.
  295. 6.4 Hiding credentials
  296. Do note that when a program is run, its parameters might be possible to see
  297. when listing the running processes of the system. Thus, other users may be
  298. able to watch your passwords if you pass them as plain command line
  299. options. There are ways to circumvent this.
  300. It is worth noting that while this is how HTTP Authentication works, very
  301. many web sites will not use this concept when they provide logins etc. See
  302. the Web Login chapter further below for more details on that.
  303. 7. More HTTP Headers
  304. 7.1 Referer
  305. A HTTP request may include a 'referer' field (yes it is misspelled), which
  306. can be used to tell from which URL the client got to this particular
  307. resource. Some programs/scripts check the referer field of requests to verify
  308. that this wasn't arriving from an external site or an unknown page. While
  309. this is a stupid way to check something so easily forged, many scripts still
  310. do it. Using curl, you can put anything you want in the referer-field and
  311. thus more easily be able to fool the server into serving your request.
  312. Use curl to set the referer field with:
  313. curl --referer http://www.example.come http://www.example.com
  314. 7.2 User Agent
  315. Very similar to the referer field, all HTTP requests may set the User-Agent
  316. field. It names what user agent (client) that is being used. Many
  317. applications use this information to decide how to display pages. Silly web
  318. programmers try to make different pages for users of different browsers to
  319. make them look the best possible for their particular browsers. They usually
  320. also do different kinds of javascript, vbscript etc.
  321. At times, you will see that getting a page with curl will not return the same
  322. page that you see when getting the page with your browser. Then you know it
  323. is time to set the User Agent field to fool the server into thinking you're
  324. one of those browsers.
  325. To make curl look like Internet Explorer 5 on a Windows 2000 box:
  326. curl --user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" [URL]
  327. Or why not look like you're using Netscape 4.73 on an old Linux box:
  328. curl --user-agent "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" [URL]
  329. 8. Redirects
  330. 8.1 Location header
  331. When a resource is requested from a server, the reply from the server may
  332. include a hint about where the browser should go next to find this page, or a
  333. new page keeping newly generated output. The header that tells the browser
  334. to redirect is Location:.
  335. Curl does not follow Location: headers by default, but will simply display
  336. such pages in the same manner it display all HTTP replies. It does however
  337. feature an option that will make it attempt to follow the Location: pointers.
  338. To tell curl to follow a Location:
  339. curl --location http://www.example.com
  340. If you use curl to POST to a site that immediately redirects you to another
  341. page, you can safely use --location (-L) and --data/--form together. Curl will
  342. only use POST in the first request, and then revert to GET in the following
  343. operations.
  344. 8.2 Other redirects
  345. Browser typically support at least two other ways of redirects that curl
  346. doesn't: first the html may contain a meta refresh tag that asks the browser
  347. to load a specific URL after a set number of seconds, or it may use
  348. javascript to do it.
  349. 9. Cookies
  350. 9.1 Cookie Basics
  351. The way the web browsers do "client side state control" is by using
  352. cookies. Cookies are just names with associated contents. The cookies are
  353. sent to the client by the server. The server tells the client for what path
  354. and host name it wants the cookie sent back, and it also sends an expiration
  355. date and a few more properties.
  356. When a client communicates with a server with a name and path as previously
  357. specified in a received cookie, the client sends back the cookies and their
  358. contents to the server, unless of course they are expired.
  359. Many applications and servers use this method to connect a series of requests
  360. into a single logical session. To be able to use curl in such occasions, we
  361. must be able to record and send back cookies the way the web application
  362. expects them. The same way browsers deal with them.
  363. 9.2 Cookie options
  364. The simplest way to send a few cookies to the server when getting a page with
  365. curl is to add them on the command line like:
  366. curl --cookie "name=Daniel" http://www.example.com
  367. Cookies are sent as common HTTP headers. This is practical as it allows curl
  368. to record cookies simply by recording headers. Record cookies with curl by
  369. using the --dump-header (-D) option like:
  370. curl --dump-header headers_and_cookies http://www.example.com
  371. (Take note that the --cookie-jar option described below is a better way to
  372. store cookies.)
  373. Curl has a full blown cookie parsing engine built-in that comes to use if you
  374. want to reconnect to a server and use cookies that were stored from a
  375. previous connection (or hand-crafted manually to fool the server into
  376. believing you had a previous connection). To use previously stored cookies,
  377. you run curl like:
  378. curl --cookie stored_cookies_in_file http://www.example.com
  379. Curl's "cookie engine" gets enabled when you use the --cookie option. If you
  380. only want curl to understand received cookies, use --cookie with a file that
  381. doesn't exist. Example, if you want to let curl understand cookies from a
  382. page and follow a location (and thus possibly send back cookies it received),
  383. you can invoke it like:
  384. curl --cookie nada --location http://www.example.com
  385. Curl has the ability to read and write cookie files that use the same file
  386. format that Netscape and Mozilla once used. It is a convenient way to share
  387. cookies between scripts or invokes. The --cookie (-b) switch automatically
  388. detects if a given file is such a cookie file and parses it, and by using the
  389. --cookie-jar (-c) option you'll make curl write a new cookie file at the end
  390. of an operation:
  391. curl --cookie cookies.txt --cookie-jar newcookies.txt \
  392. http://www.example.com
  393. 10. HTTPS
  394. 10.1 HTTPS is HTTP secure
  395. There are a few ways to do secure HTTP transfers. The by far most common
  396. protocol for doing this is what is generally known as HTTPS, HTTP over
  397. SSL. SSL encrypts all the data that is sent and received over the network and
  398. thus makes it harder for attackers to spy on sensitive information.
  399. SSL (or TLS as the latest version of the standard is called) offers a
  400. truckload of advanced features to allow all those encryptions and key
  401. infrastructure mechanisms encrypted HTTP requires.
  402. Curl supports encrypted fetches thanks to the freely available OpenSSL
  403. libraries. To get a page from a HTTPS server, simply run curl like:
  404. curl https://secure.example.com
  405. 10.2 Certificates
  406. In the HTTPS world, you use certificates to validate that you are the one
  407. you claim to be, as an addition to normal passwords. Curl supports client-
  408. side certificates. All certificates are locked with a pass phrase, which you
  409. need to enter before the certificate can be used by curl. The pass phrase
  410. can be specified on the command line or if not, entered interactively when
  411. curl queries for it. Use a certificate with curl on a HTTPS server like:
  412. curl --cert mycert.pem https://secure.example.com
  413. curl also tries to verify that the server is who it claims to be, by
  414. verifying the server's certificate against a locally stored CA cert
  415. bundle. Failing the verification will cause curl to deny the connection. You
  416. must then use --insecure (-k) in case you want to tell curl to ignore that
  417. the server can't be verified.
  418. More about server certificate verification and ca cert bundles can be read
  419. in the SSLCERTS document, available online here:
  420. http://curl.haxx.se/docs/sslcerts.html
  421. 11. Custom Request Elements
  422. 11.1 Modify method and headers
  423. Doing fancy stuff, you may need to add or change elements of a single curl
  424. request.
  425. For example, you can change the POST request to a PROPFIND and send the data
  426. as "Content-Type: text/xml" (instead of the default Content-Type) like this:
  427. curl --data "<xml>" --header "Content-Type: text/xml" \
  428. --request PROPFIND url.com
  429. You can delete a default header by providing one without content. Like you
  430. can ruin the request by chopping off the Host: header:
  431. curl --header "Host:" http://www.example.com
  432. You can add headers the same way. Your server may want a "Destination:"
  433. header, and you can add it:
  434. curl --header "Destination: http://nowhere" http://example.com
  435. 11.2 More on changed methods
  436. It should be noted that curl selects which methods to use on its own
  437. depending on what action to ask for. -d will do POST, -I will do HEAD and so
  438. on. If you use the --request / -X option you can change the method keyword
  439. curl selects, but you will not modify curl's behavior. This means that if you
  440. for example use -d "data" to do a POST, you can modify the method to a
  441. PROPFIND with -X and curl will still think it sends a POST. You can change
  442. the normal GET to a POST method by simply adding -X POST in a command line
  443. like:
  444. curl -X POST http://example.org/
  445. ... but curl will still think and act as if it sent a GET so it won't send any
  446. request body etc.
  447. 12. Web Login
  448. 12.1 Some login tricks
  449. While not strictly just HTTP related, it still cause a lot of people problems
  450. so here's the executive run-down of how the vast majority of all login forms
  451. work and how to login to them using curl.
  452. It can also be noted that to do this properly in an automated fashion, you
  453. will most certainly need to script things and do multiple curl invokes etc.
  454. First, servers mostly use cookies to track the logged-in status of the
  455. client, so you will need to capture the cookies you receive in the
  456. responses. Then, many sites also set a special cookie on the login page (to
  457. make sure you got there through their login page) so you should make a habit
  458. of first getting the login-form page to capture the cookies set there.
  459. Some web-based login systems features various amounts of javascript, and
  460. sometimes they use such code to set or modify cookie contents. Possibly they
  461. do that to prevent programmed logins, like this manual describes how to...
  462. Anyway, if reading the code isn't enough to let you repeat the behavior
  463. manually, capturing the HTTP requests done by your browsers and analyzing the
  464. sent cookies is usually a working method to work out how to shortcut the
  465. javascript need.
  466. In the actual <form> tag for the login, lots of sites fill-in random/session
  467. or otherwise secretly generated hidden tags and you may need to first capture
  468. the HTML code for the login form and extract all the hidden fields to be able
  469. to do a proper login POST. Remember that the contents need to be URL encoded
  470. when sent in a normal POST.
  471. 13. Debug
  472. 13.1 Some debug tricks
  473. Many times when you run curl on a site, you'll notice that the site doesn't
  474. seem to respond the same way to your curl requests as it does to your
  475. browser's.
  476. Then you need to start making your curl requests more similar to your
  477. browser's requests:
  478. * Use the --trace-ascii option to store fully detailed logs of the requests
  479. for easier analyzing and better understanding
  480. * Make sure you check for and use cookies when needed (both reading with
  481. --cookie and writing with --cookie-jar)
  482. * Set user-agent to one like a recent popular browser does
  483. * Set referer like it is set by the browser
  484. * If you use POST, make sure you send all the fields and in the same order as
  485. the browser does it.
  486. A very good helper to make sure you do this right, is the LiveHTTPHeader tool
  487. that lets you view all headers you send and receive with Mozilla/Firefox
  488. (even when using HTTPS). Chrome features similar functionality out of the box
  489. among the developer's tools.
  490. A more raw approach is to capture the HTTP traffic on the network with tools
  491. such as ethereal or tcpdump and check what headers that were sent and
  492. received by the browser. (HTTPS makes this technique inefficient.)
  493. 14. References
  494. 14.1 Standards
  495. RFC 2616 is a must to read if you want in-depth understanding of the HTTP
  496. protocol
  497. RFC 3986 explains the URL syntax
  498. RFC 1867 defines the HTTP post upload format
  499. RFC 6525 defines how HTTP cookies work
  500. 14.2 Sites
  501. http://curl.haxx.se is the home of the cURL project