xref: /aosp_15_r20/external/curl/docs/TheArtOfHttpScripting.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker<!--
2*6236dae4SAndroid Build Coastguard WorkerCopyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard Worker
4*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
5*6236dae4SAndroid Build Coastguard Worker-->
6*6236dae4SAndroid Build Coastguard Worker
7*6236dae4SAndroid Build Coastguard Worker# The Art Of Scripting HTTP Requests Using Curl
8*6236dae4SAndroid Build Coastguard Worker
9*6236dae4SAndroid Build Coastguard Worker## Background
10*6236dae4SAndroid Build Coastguard Worker
11*6236dae4SAndroid Build Coastguard Worker This document assumes that you are familiar with HTML and general networking.
12*6236dae4SAndroid Build Coastguard Worker
13*6236dae4SAndroid Build Coastguard Worker The increasing amount of applications moving to the web has made "HTTP
14*6236dae4SAndroid Build Coastguard Worker Scripting" more frequently requested and wanted. To be able to automatically
15*6236dae4SAndroid Build Coastguard Worker extract information from the web, to fake users, to post or upload data to
16*6236dae4SAndroid Build Coastguard Worker web servers are all important tasks today.
17*6236dae4SAndroid Build Coastguard Worker
18*6236dae4SAndroid Build Coastguard Worker Curl is a command line tool for doing all sorts of URL manipulations and
19*6236dae4SAndroid Build Coastguard Worker transfers, but this particular document focuses on how to use it when doing
20*6236dae4SAndroid Build Coastguard Worker HTTP requests for fun and profit. This documents assumes that you know how to
21*6236dae4SAndroid Build Coastguard Worker invoke `curl --help` or `curl --manual` to get basic information about it.
22*6236dae4SAndroid Build Coastguard Worker
23*6236dae4SAndroid Build Coastguard Worker Curl is not written to do everything for you. It makes the requests, it gets
24*6236dae4SAndroid Build Coastguard Worker the data, it sends data and it retrieves the information. You probably need
25*6236dae4SAndroid Build Coastguard Worker to glue everything together using some kind of script language or repeated
26*6236dae4SAndroid Build Coastguard Worker manual invokes.
27*6236dae4SAndroid Build Coastguard Worker
28*6236dae4SAndroid Build Coastguard Worker## The HTTP Protocol
29*6236dae4SAndroid Build Coastguard Worker
30*6236dae4SAndroid Build Coastguard Worker HTTP is the protocol used to fetch data from web servers. It is a simple
31*6236dae4SAndroid Build Coastguard Worker protocol that is built upon TCP/IP. The protocol also allows information to
32*6236dae4SAndroid Build Coastguard Worker get sent to the server from the client using a few different methods, as is
33*6236dae4SAndroid Build Coastguard Worker shown here.
34*6236dae4SAndroid Build Coastguard Worker
35*6236dae4SAndroid Build Coastguard Worker HTTP is plain ASCII text lines being sent by the client to a server to
36*6236dae4SAndroid Build Coastguard Worker request a particular action, and then the server replies a few text lines
37*6236dae4SAndroid Build Coastguard Worker before the actual requested content is sent to the client.
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard Worker The client, curl, sends an HTTP request. The request contains a method (like
40*6236dae4SAndroid Build Coastguard Worker GET, POST, HEAD etc), a number of request headers and sometimes a request
41*6236dae4SAndroid Build Coastguard Worker body. The HTTP server responds with a status line (indicating if things went
42*6236dae4SAndroid Build Coastguard Worker well), response headers and most often also a response body. The "body" part
43*6236dae4SAndroid Build Coastguard Worker is the plain data you requested, like the actual HTML or the image etc.
44*6236dae4SAndroid Build Coastguard Worker
45*6236dae4SAndroid Build Coastguard Worker## See the Protocol
46*6236dae4SAndroid Build Coastguard Worker
47*6236dae4SAndroid Build Coastguard Worker Using curl's option [`--verbose`](https://curl.se/docs/manpage.html#-v) (`-v`
48*6236dae4SAndroid Build Coastguard Worker as a short option) displays what kind of commands curl sends to the server,
49*6236dae4SAndroid Build Coastguard Worker as well as a few other informational texts.
50*6236dae4SAndroid Build Coastguard Worker
51*6236dae4SAndroid Build Coastguard Worker `--verbose` is the single most useful option when it comes to debug or even
52*6236dae4SAndroid Build Coastguard Worker understand the curl<->server interaction.
53*6236dae4SAndroid Build Coastguard Worker
54*6236dae4SAndroid Build Coastguard Worker Sometimes even `--verbose` is not enough. Then
55*6236dae4SAndroid Build Coastguard Worker [`--trace`](https://curl.se/docs/manpage.html#-trace) and
56*6236dae4SAndroid Build Coastguard Worker [`--trace-ascii`](https://curl.se/docs/manpage.html#--trace-ascii)
57*6236dae4SAndroid Build Coastguard Worker offer even more details as they show **everything** curl sends and
58*6236dae4SAndroid Build Coastguard Worker receives. Use it like this:
59*6236dae4SAndroid Build Coastguard Worker
60*6236dae4SAndroid Build Coastguard Worker    curl --trace-ascii debugdump.txt http://www.example.com/
61*6236dae4SAndroid Build Coastguard Worker
62*6236dae4SAndroid Build Coastguard Worker## See the Timing
63*6236dae4SAndroid Build Coastguard Worker
64*6236dae4SAndroid Build Coastguard Worker Many times you may wonder what exactly is taking all the time, or you just
65*6236dae4SAndroid Build Coastguard Worker want to know the amount of milliseconds between two points in a transfer. For
66*6236dae4SAndroid Build Coastguard Worker those, and other similar situations, the
67*6236dae4SAndroid Build Coastguard Worker [`--trace-time`](https://curl.se/docs/manpage.html#--trace-time) option is
68*6236dae4SAndroid Build Coastguard Worker what you need. It prepends the time to each trace output line:
69*6236dae4SAndroid Build Coastguard Worker
70*6236dae4SAndroid Build Coastguard Worker    curl --trace-ascii d.txt --trace-time http://example.com/
71*6236dae4SAndroid Build Coastguard Worker
72*6236dae4SAndroid Build Coastguard Worker## See which Transfer
73*6236dae4SAndroid Build Coastguard Worker
74*6236dae4SAndroid Build Coastguard Worker When doing parallel transfers, it is relevant to see which transfer is doing
75*6236dae4SAndroid Build Coastguard Worker what. When response headers are received (and logged) you need to know which
76*6236dae4SAndroid Build Coastguard Worker transfer these are for.
77*6236dae4SAndroid Build Coastguard Worker [`--trace-ids`](https://curl.se/docs/manpage.html#--trace-ids) option is what
78*6236dae4SAndroid Build Coastguard Worker you need. It prepends the transfer and connection identifier to each trace
79*6236dae4SAndroid Build Coastguard Worker output line:
80*6236dae4SAndroid Build Coastguard Worker
81*6236dae4SAndroid Build Coastguard Worker    curl --trace-ascii d.txt --trace-ids http://example.com/
82*6236dae4SAndroid Build Coastguard Worker
83*6236dae4SAndroid Build Coastguard Worker## See the Response
84*6236dae4SAndroid Build Coastguard Worker
85*6236dae4SAndroid Build Coastguard Worker By default curl sends the response to stdout. You need to redirect it
86*6236dae4SAndroid Build Coastguard Worker somewhere to avoid that, most often that is done with `-o` or `-O`.
87*6236dae4SAndroid Build Coastguard Worker
88*6236dae4SAndroid Build Coastguard Worker# URL
89*6236dae4SAndroid Build Coastguard Worker
90*6236dae4SAndroid Build Coastguard Worker## Spec
91*6236dae4SAndroid Build Coastguard Worker
92*6236dae4SAndroid Build Coastguard Worker The Uniform Resource Locator format is how you specify the address of a
93*6236dae4SAndroid Build Coastguard Worker particular resource on the Internet. You know these, you have seen URLs like
94*6236dae4SAndroid Build Coastguard Worker https://curl.se or https://example.com a million times. RFC 3986 is the
95*6236dae4SAndroid Build Coastguard Worker canonical spec. The formal name is not URL, it is **URI**.
96*6236dae4SAndroid Build Coastguard Worker
97*6236dae4SAndroid Build Coastguard Worker## Host
98*6236dae4SAndroid Build Coastguard Worker
99*6236dae4SAndroid Build Coastguard Worker The hostname is usually resolved using DNS or your /etc/hosts file to an IP
100*6236dae4SAndroid Build Coastguard Worker address and that is what curl communicates with. Alternatively you specify
101*6236dae4SAndroid Build Coastguard Worker the IP address directly in the URL instead of a name.
102*6236dae4SAndroid Build Coastguard Worker
103*6236dae4SAndroid Build Coastguard Worker For development and other trying out situations, you can point to a different
104*6236dae4SAndroid Build Coastguard Worker IP address for a hostname than what would otherwise be used, by using curl's
105*6236dae4SAndroid Build Coastguard Worker [`--resolve`](https://curl.se/docs/manpage.html#--resolve) option:
106*6236dae4SAndroid Build Coastguard Worker
107*6236dae4SAndroid Build Coastguard Worker    curl --resolve www.example.org:80:127.0.0.1 http://www.example.org/
108*6236dae4SAndroid Build Coastguard Worker
109*6236dae4SAndroid Build Coastguard Worker## Port number
110*6236dae4SAndroid Build Coastguard Worker
111*6236dae4SAndroid Build Coastguard Worker Each protocol curl supports operates on a default port number, be it over TCP
112*6236dae4SAndroid Build Coastguard Worker or in some cases UDP. Normally you do not have to take that into
113*6236dae4SAndroid Build Coastguard Worker consideration, but at times you run test servers on other ports or
114*6236dae4SAndroid Build Coastguard Worker similar. Then you can specify the port number in the URL with a colon and a
115*6236dae4SAndroid Build Coastguard Worker number immediately following the hostname. Like when doing HTTP to port
116*6236dae4SAndroid Build Coastguard Worker 1234:
117*6236dae4SAndroid Build Coastguard Worker
118*6236dae4SAndroid Build Coastguard Worker    curl http://www.example.org:1234/
119*6236dae4SAndroid Build Coastguard Worker
120*6236dae4SAndroid Build Coastguard Worker The port number you specify in the URL is the number that the server uses to
121*6236dae4SAndroid Build Coastguard Worker offer its services. Sometimes you may use a proxy, and then you may
122*6236dae4SAndroid Build Coastguard Worker need to specify that proxy's port number separately from what curl needs to
123*6236dae4SAndroid Build Coastguard Worker connect to the server. Like when using an HTTP proxy on port 4321:
124*6236dae4SAndroid Build Coastguard Worker
125*6236dae4SAndroid Build Coastguard Worker    curl --proxy http://proxy.example.org:4321 http://remote.example.org/
126*6236dae4SAndroid Build Coastguard Worker
127*6236dae4SAndroid Build Coastguard Worker## Username and password
128*6236dae4SAndroid Build Coastguard Worker
129*6236dae4SAndroid Build Coastguard Worker Some services are setup to require HTTP authentication and then you need to
130*6236dae4SAndroid Build Coastguard Worker provide name and password which is then transferred to the remote site in
131*6236dae4SAndroid Build Coastguard Worker various ways depending on the exact authentication protocol used.
132*6236dae4SAndroid Build Coastguard Worker
133*6236dae4SAndroid Build Coastguard Worker You can opt to either insert the user and password in the URL or you can
134*6236dae4SAndroid Build Coastguard Worker provide them separately:
135*6236dae4SAndroid Build Coastguard Worker
136*6236dae4SAndroid Build Coastguard Worker    curl http://user:[email protected]/
137*6236dae4SAndroid Build Coastguard Worker
138*6236dae4SAndroid Build Coastguard Worker or
139*6236dae4SAndroid Build Coastguard Worker
140*6236dae4SAndroid Build Coastguard Worker    curl -u user:password http://example.org/
141*6236dae4SAndroid Build Coastguard Worker
142*6236dae4SAndroid Build Coastguard Worker You need to pay attention that this kind of HTTP authentication is not what
143*6236dae4SAndroid Build Coastguard Worker is usually done and requested by user-oriented websites these days. They tend
144*6236dae4SAndroid Build Coastguard Worker to use forms and cookies instead.
145*6236dae4SAndroid Build Coastguard Worker
146*6236dae4SAndroid Build Coastguard Worker## Path part
147*6236dae4SAndroid Build Coastguard Worker
148*6236dae4SAndroid Build Coastguard Worker The path part is just sent off to the server to request that it sends back
149*6236dae4SAndroid Build Coastguard Worker the associated response. The path is what is to the right side of the slash
150*6236dae4SAndroid Build Coastguard Worker that follows the hostname and possibly port number.
151*6236dae4SAndroid Build Coastguard Worker
152*6236dae4SAndroid Build Coastguard Worker# Fetch a page
153*6236dae4SAndroid Build Coastguard Worker
154*6236dae4SAndroid Build Coastguard Worker## GET
155*6236dae4SAndroid Build Coastguard Worker
156*6236dae4SAndroid Build Coastguard Worker The simplest and most common request/operation made using HTTP is to GET a
157*6236dae4SAndroid Build Coastguard Worker URL. The URL could itself refer to a webpage, an image or a file. The client
158*6236dae4SAndroid Build Coastguard Worker issues a GET request to the server and receives the document it asked for.
159*6236dae4SAndroid Build Coastguard Worker If you issue the command line
160*6236dae4SAndroid Build Coastguard Worker
161*6236dae4SAndroid Build Coastguard Worker    curl https://curl.se
162*6236dae4SAndroid Build Coastguard Worker
163*6236dae4SAndroid Build Coastguard Worker you get a webpage returned in your terminal window. The entire HTML document
164*6236dae4SAndroid Build Coastguard Worker that that URL holds.
165*6236dae4SAndroid Build Coastguard Worker
166*6236dae4SAndroid Build Coastguard Worker All HTTP replies contain a set of response headers that are normally hidden,
167*6236dae4SAndroid Build Coastguard Worker use curl's [`--include`](https://curl.se/docs/manpage.html#-i) (`-i`)
168*6236dae4SAndroid Build Coastguard Worker option to display them as well as the rest of the document.
169*6236dae4SAndroid Build Coastguard Worker
170*6236dae4SAndroid Build Coastguard Worker## HEAD
171*6236dae4SAndroid Build Coastguard Worker
172*6236dae4SAndroid Build Coastguard Worker You can ask the remote server for ONLY the headers by using the
173*6236dae4SAndroid Build Coastguard Worker [`--head`](https://curl.se/docs/manpage.html#-I) (`-I`) option which makes
174*6236dae4SAndroid Build Coastguard Worker curl issue a HEAD request. In some special cases servers deny the HEAD method
175*6236dae4SAndroid Build Coastguard Worker while others still work, which is a particular kind of annoyance.
176*6236dae4SAndroid Build Coastguard Worker
177*6236dae4SAndroid Build Coastguard Worker The HEAD method is defined and made so that the server returns the headers
178*6236dae4SAndroid Build Coastguard Worker exactly the way it would do for a GET, but without a body. It means that you
179*6236dae4SAndroid Build Coastguard Worker may see a `Content-Length:` in the response headers, but there must not be an
180*6236dae4SAndroid Build Coastguard Worker actual body in the HEAD response.
181*6236dae4SAndroid Build Coastguard Worker
182*6236dae4SAndroid Build Coastguard Worker## Multiple URLs in a single command line
183*6236dae4SAndroid Build Coastguard Worker
184*6236dae4SAndroid Build Coastguard Worker A single curl command line may involve one or many URLs. The most common case
185*6236dae4SAndroid Build Coastguard Worker is probably to just use one, but you can specify any amount of URLs. Yes any.
186*6236dae4SAndroid Build Coastguard Worker No limits. You then get requests repeated over and over for all the given
187*6236dae4SAndroid Build Coastguard Worker URLs.
188*6236dae4SAndroid Build Coastguard Worker
189*6236dae4SAndroid Build Coastguard Worker Example, send two GET requests:
190*6236dae4SAndroid Build Coastguard Worker
191*6236dae4SAndroid Build Coastguard Worker    curl http://url1.example.com http://url2.example.com
192*6236dae4SAndroid Build Coastguard Worker
193*6236dae4SAndroid Build Coastguard Worker If you use [`--data`](https://curl.se/docs/manpage.html#-d) to POST to
194*6236dae4SAndroid Build Coastguard Worker the URL, using multiple URLs means that you send that same POST to all the
195*6236dae4SAndroid Build Coastguard Worker given URLs.
196*6236dae4SAndroid Build Coastguard Worker
197*6236dae4SAndroid Build Coastguard Worker Example, send two POSTs:
198*6236dae4SAndroid Build Coastguard Worker
199*6236dae4SAndroid Build Coastguard Worker    curl --data name=curl http://url1.example.com http://url2.example.com
200*6236dae4SAndroid Build Coastguard Worker
201*6236dae4SAndroid Build Coastguard Worker
202*6236dae4SAndroid Build Coastguard Worker## Multiple HTTP methods in a single command line
203*6236dae4SAndroid Build Coastguard Worker
204*6236dae4SAndroid Build Coastguard Worker Sometimes you need to operate on several URLs in a single command line and do
205*6236dae4SAndroid Build Coastguard Worker different HTTP methods on each. For this, you might enjoy the
206*6236dae4SAndroid Build Coastguard Worker [`--next`](https://curl.se/docs/manpage.html#-:) option. It is basically a
207*6236dae4SAndroid Build Coastguard Worker separator that separates a bunch of options from the next. All the URLs
208*6236dae4SAndroid Build Coastguard Worker before `--next` get the same method and get all the POST data merged into
209*6236dae4SAndroid Build Coastguard Worker one.
210*6236dae4SAndroid Build Coastguard Worker
211*6236dae4SAndroid Build Coastguard Worker When curl reaches the `--next` on the command line, it resets the method and
212*6236dae4SAndroid Build Coastguard Worker the POST data and allow a new set.
213*6236dae4SAndroid Build Coastguard Worker
214*6236dae4SAndroid Build Coastguard Worker Perhaps this is best shown with a few examples. To send first a HEAD and then
215*6236dae4SAndroid Build Coastguard Worker a GET:
216*6236dae4SAndroid Build Coastguard Worker
217*6236dae4SAndroid Build Coastguard Worker    curl -I http://example.com --next http://example.com
218*6236dae4SAndroid Build Coastguard Worker
219*6236dae4SAndroid Build Coastguard Worker To first send a POST and then a GET:
220*6236dae4SAndroid Build Coastguard Worker
221*6236dae4SAndroid Build Coastguard Worker    curl -d score=10 http://example.com/post.cgi --next http://example.com/results.html
222*6236dae4SAndroid Build Coastguard Worker
223*6236dae4SAndroid Build Coastguard Worker# HTML forms
224*6236dae4SAndroid Build Coastguard Worker
225*6236dae4SAndroid Build Coastguard Worker## Forms explained
226*6236dae4SAndroid Build Coastguard Worker
227*6236dae4SAndroid Build Coastguard Worker Forms are the general way a website can present an HTML page with fields for
228*6236dae4SAndroid Build Coastguard Worker the user to enter data in, and then press some kind of 'OK' or 'Submit'
229*6236dae4SAndroid Build Coastguard Worker button to get that data sent to the server. The server then typically uses
230*6236dae4SAndroid Build Coastguard Worker the posted data to decide how to act. Like using the entered words to search
231*6236dae4SAndroid Build Coastguard Worker in a database, or to add the info in a bug tracking system, display the
232*6236dae4SAndroid Build Coastguard Worker entered address on a map or using the info as a login-prompt verifying that
233*6236dae4SAndroid Build Coastguard Worker the user is allowed to see what it is about to see.
234*6236dae4SAndroid Build Coastguard Worker
235*6236dae4SAndroid Build Coastguard Worker Of course there has to be some kind of program on the server end to receive
236*6236dae4SAndroid Build Coastguard Worker the data you send. You cannot just invent something out of the air.
237*6236dae4SAndroid Build Coastguard Worker
238*6236dae4SAndroid Build Coastguard Worker## GET
239*6236dae4SAndroid Build Coastguard Worker
240*6236dae4SAndroid Build Coastguard Worker A GET-form uses the method GET, as specified in HTML like:
241*6236dae4SAndroid Build Coastguard Worker
242*6236dae4SAndroid Build Coastguard Worker```html
243*6236dae4SAndroid Build Coastguard Worker<form method="GET" action="junk.cgi">
244*6236dae4SAndroid Build Coastguard Worker  <input type=text name="birthyear">
245*6236dae4SAndroid Build Coastguard Worker  <input type=submit name=press value="OK">
246*6236dae4SAndroid Build Coastguard Worker</form>
247*6236dae4SAndroid Build Coastguard Worker```
248*6236dae4SAndroid Build Coastguard Worker
249*6236dae4SAndroid Build Coastguard Worker In your favorite browser, this form appears with a text box to fill in and a
250*6236dae4SAndroid Build Coastguard Worker press-button labeled "OK". If you fill in '1905' and press the OK button,
251*6236dae4SAndroid Build Coastguard Worker your browser then creates a new URL to get for you. The URL gets
252*6236dae4SAndroid Build Coastguard Worker `junk.cgi?birthyear=1905&press=OK` appended to the path part of the previous
253*6236dae4SAndroid Build Coastguard Worker URL.
254*6236dae4SAndroid Build Coastguard Worker
255*6236dae4SAndroid Build Coastguard Worker If the original form was seen on the page `www.example.com/when/birth.html`,
256*6236dae4SAndroid Build Coastguard Worker the second page you get becomes
257*6236dae4SAndroid Build Coastguard Worker `www.example.com/when/junk.cgi?birthyear=1905&press=OK`.
258*6236dae4SAndroid Build Coastguard Worker
259*6236dae4SAndroid Build Coastguard Worker Most search engines work this way.
260*6236dae4SAndroid Build Coastguard Worker
261*6236dae4SAndroid Build Coastguard Worker To make curl do the GET form post for you, just enter the expected created
262*6236dae4SAndroid Build Coastguard Worker URL:
263*6236dae4SAndroid Build Coastguard Worker
264*6236dae4SAndroid Build Coastguard Worker    curl "http://www.example.com/when/junk.cgi?birthyear=1905&press=OK"
265*6236dae4SAndroid Build Coastguard Worker
266*6236dae4SAndroid Build Coastguard Worker## POST
267*6236dae4SAndroid Build Coastguard Worker
268*6236dae4SAndroid Build Coastguard Worker The GET method makes all input field names get displayed in the URL field of
269*6236dae4SAndroid Build Coastguard Worker your browser. That is generally a good thing when you want to be able to
270*6236dae4SAndroid Build Coastguard Worker bookmark that page with your given data, but it is an obvious disadvantage if
271*6236dae4SAndroid Build Coastguard Worker you entered secret information in one of the fields or if there are a large
272*6236dae4SAndroid Build Coastguard Worker amount of fields creating a long and unreadable URL.
273*6236dae4SAndroid Build Coastguard Worker
274*6236dae4SAndroid Build Coastguard Worker The HTTP protocol then offers the POST method. This way the client sends the
275*6236dae4SAndroid Build Coastguard Worker data separated from the URL and thus you do not see any of it in the URL
276*6236dae4SAndroid Build Coastguard Worker address field.
277*6236dae4SAndroid Build Coastguard Worker
278*6236dae4SAndroid Build Coastguard Worker The form would look similar to the previous one:
279*6236dae4SAndroid Build Coastguard Worker
280*6236dae4SAndroid Build Coastguard Worker```html
281*6236dae4SAndroid Build Coastguard Worker<form method="POST" action="junk.cgi">
282*6236dae4SAndroid Build Coastguard Worker  <input type=text name="birthyear">
283*6236dae4SAndroid Build Coastguard Worker  <input type=submit name=press value=" OK ">
284*6236dae4SAndroid Build Coastguard Worker</form>
285*6236dae4SAndroid Build Coastguard Worker```
286*6236dae4SAndroid Build Coastguard Worker
287*6236dae4SAndroid Build Coastguard Worker And to use curl to post this form with the same data filled in as before, we
288*6236dae4SAndroid Build Coastguard Worker could do it like:
289*6236dae4SAndroid Build Coastguard Worker
290*6236dae4SAndroid Build Coastguard Worker    curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when/junk.cgi
291*6236dae4SAndroid Build Coastguard Worker
292*6236dae4SAndroid Build Coastguard Worker This kind of POST uses the Content-Type `application/x-www-form-urlencoded`
293*6236dae4SAndroid Build Coastguard Worker and is the most widely used POST kind.
294*6236dae4SAndroid Build Coastguard Worker
295*6236dae4SAndroid Build Coastguard Worker The data you send to the server MUST already be properly encoded, curl does
296*6236dae4SAndroid Build Coastguard Worker not do that for you. For example, if you want the data to contain a space,
297*6236dae4SAndroid Build Coastguard Worker you need to replace that space with `%20`, etc. Failing to comply with this
298*6236dae4SAndroid Build Coastguard Worker most likely causes your data to be received wrongly and messed up.
299*6236dae4SAndroid Build Coastguard Worker
300*6236dae4SAndroid Build Coastguard Worker Recent curl versions can in fact url-encode POST data for you, like this:
301*6236dae4SAndroid Build Coastguard Worker
302*6236dae4SAndroid Build Coastguard Worker    curl --data-urlencode "name=I am Daniel" http://www.example.com
303*6236dae4SAndroid Build Coastguard Worker
304*6236dae4SAndroid Build Coastguard Worker If you repeat `--data` several times on the command line, curl concatenates
305*6236dae4SAndroid Build Coastguard Worker all the given data pieces - and put a `&` symbol between each data segment.
306*6236dae4SAndroid Build Coastguard Worker
307*6236dae4SAndroid Build Coastguard Worker## File Upload POST
308*6236dae4SAndroid Build Coastguard Worker
309*6236dae4SAndroid Build Coastguard Worker Back in late 1995 they defined an additional way to post data over HTTP. It
310*6236dae4SAndroid Build Coastguard Worker is documented in the RFC 1867, why this method sometimes is referred to as
311*6236dae4SAndroid Build Coastguard Worker RFC 1867-posting.
312*6236dae4SAndroid Build Coastguard Worker
313*6236dae4SAndroid Build Coastguard Worker This method is mainly designed to better support file uploads. A form that
314*6236dae4SAndroid Build Coastguard Worker allows a user to upload a file could be written like this in HTML:
315*6236dae4SAndroid Build Coastguard Worker
316*6236dae4SAndroid Build Coastguard Worker    <form method="POST" enctype='multipart/form-data' action="upload.cgi">
317*6236dae4SAndroid Build Coastguard Worker      <input name=upload type=file>
318*6236dae4SAndroid Build Coastguard Worker      <input type=submit name=press value="OK">
319*6236dae4SAndroid Build Coastguard Worker    </form>
320*6236dae4SAndroid Build Coastguard Worker
321*6236dae4SAndroid Build Coastguard Worker This clearly shows that the Content-Type about to be sent is
322*6236dae4SAndroid Build Coastguard Worker `multipart/form-data`.
323*6236dae4SAndroid Build Coastguard Worker
324*6236dae4SAndroid Build Coastguard Worker To post to a form like this with curl, you enter a command line like:
325*6236dae4SAndroid Build Coastguard Worker
326*6236dae4SAndroid Build Coastguard Worker    curl --form upload=@localfilename --form press=OK [URL]
327*6236dae4SAndroid Build Coastguard Worker
328*6236dae4SAndroid Build Coastguard Worker## Hidden Fields
329*6236dae4SAndroid Build Coastguard Worker
330*6236dae4SAndroid Build Coastguard Worker A common way for HTML based applications to pass state information between
331*6236dae4SAndroid Build Coastguard Worker pages is to add hidden fields to the forms. Hidden fields are already filled
332*6236dae4SAndroid Build Coastguard Worker in, they are not displayed to the user and they get passed along just as all
333*6236dae4SAndroid Build Coastguard Worker the other fields.
334*6236dae4SAndroid Build Coastguard Worker
335*6236dae4SAndroid Build Coastguard Worker A similar example form with one visible field, one hidden field and one
336*6236dae4SAndroid Build Coastguard Worker submit button could look like:
337*6236dae4SAndroid Build Coastguard Worker
338*6236dae4SAndroid Build Coastguard Worker```html
339*6236dae4SAndroid Build Coastguard Worker<form method="POST" action="foobar.cgi">
340*6236dae4SAndroid Build Coastguard Worker  <input type=text name="birthyear">
341*6236dae4SAndroid Build Coastguard Worker  <input type=hidden name="person" value="daniel">
342*6236dae4SAndroid Build Coastguard Worker  <input type=submit name="press" value="OK">
343*6236dae4SAndroid Build Coastguard Worker</form>
344*6236dae4SAndroid Build Coastguard Worker```
345*6236dae4SAndroid Build Coastguard Worker
346*6236dae4SAndroid Build Coastguard Worker To POST this with curl, you do not have to think about if the fields are
347*6236dae4SAndroid Build Coastguard Worker hidden or not. To curl they are all the same:
348*6236dae4SAndroid Build Coastguard Worker
349*6236dae4SAndroid Build Coastguard Worker    curl --data "birthyear=1905&press=OK&person=daniel" [URL]
350*6236dae4SAndroid Build Coastguard Worker
351*6236dae4SAndroid Build Coastguard Worker## Figure Out What A POST Looks Like
352*6236dae4SAndroid Build Coastguard Worker
353*6236dae4SAndroid Build Coastguard Worker When you are about to fill in a form and send it to a server by using curl
354*6236dae4SAndroid Build Coastguard Worker instead of a browser, you are of course interested in sending a POST exactly
355*6236dae4SAndroid Build Coastguard Worker the way your browser does.
356*6236dae4SAndroid Build Coastguard Worker
357*6236dae4SAndroid Build Coastguard Worker An easy way to get to see this, is to save the HTML page with the form on
358*6236dae4SAndroid Build Coastguard Worker your local disk, modify the 'method' to a GET, and press the submit button
359*6236dae4SAndroid Build Coastguard Worker (you could also change the action URL if you want to).
360*6236dae4SAndroid Build Coastguard Worker
361*6236dae4SAndroid Build Coastguard Worker You then clearly see the data get appended to the URL, separated with a
362*6236dae4SAndroid Build Coastguard Worker `?`-letter as GET forms are supposed to.
363*6236dae4SAndroid Build Coastguard Worker
364*6236dae4SAndroid Build Coastguard Worker# HTTP upload
365*6236dae4SAndroid Build Coastguard Worker
366*6236dae4SAndroid Build Coastguard Worker## PUT
367*6236dae4SAndroid Build Coastguard Worker
368*6236dae4SAndroid Build Coastguard Worker Perhaps the best way to upload data to an HTTP server is to use PUT. Then
369*6236dae4SAndroid Build Coastguard Worker again, this of course requires that someone put a program or script on the
370*6236dae4SAndroid Build Coastguard Worker server end that knows how to receive an HTTP PUT stream.
371*6236dae4SAndroid Build Coastguard Worker
372*6236dae4SAndroid Build Coastguard Worker Put a file to an HTTP server with curl:
373*6236dae4SAndroid Build Coastguard Worker
374*6236dae4SAndroid Build Coastguard Worker    curl --upload-file uploadfile http://www.example.com/receive.cgi
375*6236dae4SAndroid Build Coastguard Worker
376*6236dae4SAndroid Build Coastguard Worker# HTTP Authentication
377*6236dae4SAndroid Build Coastguard Worker
378*6236dae4SAndroid Build Coastguard Worker## Basic Authentication
379*6236dae4SAndroid Build Coastguard Worker
380*6236dae4SAndroid Build Coastguard Worker HTTP Authentication is the ability to tell the server your username and
381*6236dae4SAndroid Build Coastguard Worker password so that it can verify that you are allowed to do the request you are
382*6236dae4SAndroid Build Coastguard Worker doing. The Basic authentication used in HTTP (which is the type curl uses by
383*6236dae4SAndroid Build Coastguard Worker default) is **plain text** based, which means it sends username and password
384*6236dae4SAndroid Build Coastguard Worker only slightly obfuscated, but still fully readable by anyone that sniffs on
385*6236dae4SAndroid Build Coastguard Worker the network between you and the remote server.
386*6236dae4SAndroid Build Coastguard Worker
387*6236dae4SAndroid Build Coastguard Worker To tell curl to use a user and password for authentication:
388*6236dae4SAndroid Build Coastguard Worker
389*6236dae4SAndroid Build Coastguard Worker    curl --user name:password http://www.example.com
390*6236dae4SAndroid Build Coastguard Worker
391*6236dae4SAndroid Build Coastguard Worker## Other Authentication
392*6236dae4SAndroid Build Coastguard Worker
393*6236dae4SAndroid Build Coastguard Worker The site might require a different authentication method (check the headers
394*6236dae4SAndroid Build Coastguard Worker returned by the server), and then
395*6236dae4SAndroid Build Coastguard Worker [`--ntlm`](https://curl.se/docs/manpage.html#--ntlm),
396*6236dae4SAndroid Build Coastguard Worker [`--digest`](https://curl.se/docs/manpage.html#--digest),
397*6236dae4SAndroid Build Coastguard Worker [`--negotiate`](https://curl.se/docs/manpage.html#--negotiate) or even
398*6236dae4SAndroid Build Coastguard Worker [`--anyauth`](https://curl.se/docs/manpage.html#--anyauth) might be
399*6236dae4SAndroid Build Coastguard Worker options that suit you.
400*6236dae4SAndroid Build Coastguard Worker
401*6236dae4SAndroid Build Coastguard Worker## Proxy Authentication
402*6236dae4SAndroid Build Coastguard Worker
403*6236dae4SAndroid Build Coastguard Worker Sometimes your HTTP access is only available through the use of an HTTP
404*6236dae4SAndroid Build Coastguard Worker proxy. This seems to be especially common at various companies. An HTTP proxy
405*6236dae4SAndroid Build Coastguard Worker may require its own user and password to allow the client to get through to
406*6236dae4SAndroid Build Coastguard Worker the Internet. To specify those with curl, run something like:
407*6236dae4SAndroid Build Coastguard Worker
408*6236dae4SAndroid Build Coastguard Worker    curl --proxy-user proxyuser:proxypassword curl.se
409*6236dae4SAndroid Build Coastguard Worker
410*6236dae4SAndroid Build Coastguard Worker If your proxy requires the authentication to be done using the NTLM method,
411*6236dae4SAndroid Build Coastguard Worker use [`--proxy-ntlm`](https://curl.se/docs/manpage.html#--proxy-ntlm), if
412*6236dae4SAndroid Build Coastguard Worker it requires Digest use
413*6236dae4SAndroid Build Coastguard Worker [`--proxy-digest`](https://curl.se/docs/manpage.html#--proxy-digest).
414*6236dae4SAndroid Build Coastguard Worker
415*6236dae4SAndroid Build Coastguard Worker If you use any one of these user+password options but leave out the password
416*6236dae4SAndroid Build Coastguard Worker part, curl prompts for the password interactively.
417*6236dae4SAndroid Build Coastguard Worker
418*6236dae4SAndroid Build Coastguard Worker## Hiding credentials
419*6236dae4SAndroid Build Coastguard Worker
420*6236dae4SAndroid Build Coastguard Worker Do note that when a program is run, its parameters might be possible to see
421*6236dae4SAndroid Build Coastguard Worker when listing the running processes of the system. Thus, other users may be
422*6236dae4SAndroid Build Coastguard Worker able to watch your passwords if you pass them as plain command line
423*6236dae4SAndroid Build Coastguard Worker options. There are ways to circumvent this.
424*6236dae4SAndroid Build Coastguard Worker
425*6236dae4SAndroid Build Coastguard Worker It is worth noting that while this is how HTTP Authentication works, many
426*6236dae4SAndroid Build Coastguard Worker websites do not use this concept when they provide logins etc. See the Web
427*6236dae4SAndroid Build Coastguard Worker Login chapter further below for more details on that.
428*6236dae4SAndroid Build Coastguard Worker
429*6236dae4SAndroid Build Coastguard Worker# More HTTP Headers
430*6236dae4SAndroid Build Coastguard Worker
431*6236dae4SAndroid Build Coastguard Worker## Referer
432*6236dae4SAndroid Build Coastguard Worker
433*6236dae4SAndroid Build Coastguard Worker An HTTP request may include a 'referer' field (yes it is misspelled), which
434*6236dae4SAndroid Build Coastguard Worker can be used to tell from which URL the client got to this particular
435*6236dae4SAndroid Build Coastguard Worker resource. Some programs/scripts check the referer field of requests to verify
436*6236dae4SAndroid Build Coastguard Worker that this was not arriving from an external site or an unknown page. While
437*6236dae4SAndroid Build Coastguard Worker this is a stupid way to check something so easily forged, many scripts still
438*6236dae4SAndroid Build Coastguard Worker do it. Using curl, you can put anything you want in the referer-field and
439*6236dae4SAndroid Build Coastguard Worker thus more easily be able to fool the server into serving your request.
440*6236dae4SAndroid Build Coastguard Worker
441*6236dae4SAndroid Build Coastguard Worker Use curl to set the referer field with:
442*6236dae4SAndroid Build Coastguard Worker
443*6236dae4SAndroid Build Coastguard Worker    curl --referer http://www.example.come http://www.example.com
444*6236dae4SAndroid Build Coastguard Worker
445*6236dae4SAndroid Build Coastguard Worker## User Agent
446*6236dae4SAndroid Build Coastguard Worker
447*6236dae4SAndroid Build Coastguard Worker Similar to the referer field, all HTTP requests may set the User-Agent
448*6236dae4SAndroid Build Coastguard Worker field. It names what user agent (client) that is being used. Many
449*6236dae4SAndroid Build Coastguard Worker applications use this information to decide how to display pages. Silly web
450*6236dae4SAndroid Build Coastguard Worker programmers try to make different pages for users of different browsers to
451*6236dae4SAndroid Build Coastguard Worker make them look the best possible for their particular browsers. They usually
452*6236dae4SAndroid Build Coastguard Worker also do different kinds of JavaScript etc.
453*6236dae4SAndroid Build Coastguard Worker
454*6236dae4SAndroid Build Coastguard Worker At times, you may learn that getting a page with curl does not return the
455*6236dae4SAndroid Build Coastguard Worker same page that you see when getting the page with your browser. Then you know
456*6236dae4SAndroid Build Coastguard Worker it is time to set the User Agent field to fool the server into thinking you
457*6236dae4SAndroid Build Coastguard Worker are one of those browsers.
458*6236dae4SAndroid Build Coastguard Worker
459*6236dae4SAndroid Build Coastguard Worker To make curl look like Internet Explorer 5 on a Windows 2000 box:
460*6236dae4SAndroid Build Coastguard Worker
461*6236dae4SAndroid Build Coastguard Worker    curl --user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" [URL]
462*6236dae4SAndroid Build Coastguard Worker
463*6236dae4SAndroid Build Coastguard Worker Or why not look like you are using Netscape 4.73 on an old Linux box:
464*6236dae4SAndroid Build Coastguard Worker
465*6236dae4SAndroid Build Coastguard Worker    curl --user-agent "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" [URL]
466*6236dae4SAndroid Build Coastguard Worker
467*6236dae4SAndroid Build Coastguard Worker## Redirects
468*6236dae4SAndroid Build Coastguard Worker
469*6236dae4SAndroid Build Coastguard Worker## Location header
470*6236dae4SAndroid Build Coastguard Worker
471*6236dae4SAndroid Build Coastguard Worker When a resource is requested from a server, the reply from the server may
472*6236dae4SAndroid Build Coastguard Worker include a hint about where the browser should go next to find this page, or a
473*6236dae4SAndroid Build Coastguard Worker new page keeping newly generated output. The header that tells the browser to
474*6236dae4SAndroid Build Coastguard Worker redirect is `Location:`.
475*6236dae4SAndroid Build Coastguard Worker
476*6236dae4SAndroid Build Coastguard Worker Curl does not follow `Location:` headers by default, but simply displays such
477*6236dae4SAndroid Build Coastguard Worker pages in the same manner it displays all HTTP replies. It does however
478*6236dae4SAndroid Build Coastguard Worker feature an option that makes it attempt to follow the `Location:` pointers.
479*6236dae4SAndroid Build Coastguard Worker
480*6236dae4SAndroid Build Coastguard Worker To tell curl to follow a Location:
481*6236dae4SAndroid Build Coastguard Worker
482*6236dae4SAndroid Build Coastguard Worker    curl --location http://www.example.com
483*6236dae4SAndroid Build Coastguard Worker
484*6236dae4SAndroid Build Coastguard Worker If you use curl to POST to a site that immediately redirects you to another
485*6236dae4SAndroid Build Coastguard Worker page, you can safely use [`--location`](https://curl.se/docs/manpage.html#-L)
486*6236dae4SAndroid Build Coastguard Worker (`-L`) and `--data`/`--form` together. Curl only uses POST in the first
487*6236dae4SAndroid Build Coastguard Worker request, and then revert to GET in the following operations.
488*6236dae4SAndroid Build Coastguard Worker
489*6236dae4SAndroid Build Coastguard Worker## Other redirects
490*6236dae4SAndroid Build Coastguard Worker
491*6236dae4SAndroid Build Coastguard Worker Browsers typically support at least two other ways of redirects that curl
492*6236dae4SAndroid Build Coastguard Worker does not: first the html may contain a meta refresh tag that asks the browser
493*6236dae4SAndroid Build Coastguard Worker to load a specific URL after a set number of seconds, or it may use
494*6236dae4SAndroid Build Coastguard Worker JavaScript to do it.
495*6236dae4SAndroid Build Coastguard Worker
496*6236dae4SAndroid Build Coastguard Worker# Cookies
497*6236dae4SAndroid Build Coastguard Worker
498*6236dae4SAndroid Build Coastguard Worker## Cookie Basics
499*6236dae4SAndroid Build Coastguard Worker
500*6236dae4SAndroid Build Coastguard Worker The way the web browsers do "client side state control" is by using
501*6236dae4SAndroid Build Coastguard Worker cookies. Cookies are just names with associated contents. The cookies are
502*6236dae4SAndroid Build Coastguard Worker sent to the client by the server. The server tells the client for what path
503*6236dae4SAndroid Build Coastguard Worker and hostname it wants the cookie sent back, and it also sends an expiration
504*6236dae4SAndroid Build Coastguard Worker date and a few more properties.
505*6236dae4SAndroid Build Coastguard Worker
506*6236dae4SAndroid Build Coastguard Worker When a client communicates with a server with a name and path as previously
507*6236dae4SAndroid Build Coastguard Worker specified in a received cookie, the client sends back the cookies and their
508*6236dae4SAndroid Build Coastguard Worker contents to the server, unless of course they are expired.
509*6236dae4SAndroid Build Coastguard Worker
510*6236dae4SAndroid Build Coastguard Worker Many applications and servers use this method to connect a series of requests
511*6236dae4SAndroid Build Coastguard Worker into a single logical session. To be able to use curl in such occasions, we
512*6236dae4SAndroid Build Coastguard Worker must be able to record and send back cookies the way the web application
513*6236dae4SAndroid Build Coastguard Worker expects them. The same way browsers deal with them.
514*6236dae4SAndroid Build Coastguard Worker
515*6236dae4SAndroid Build Coastguard Worker## Cookie options
516*6236dae4SAndroid Build Coastguard Worker
517*6236dae4SAndroid Build Coastguard Worker The simplest way to send a few cookies to the server when getting a page with
518*6236dae4SAndroid Build Coastguard Worker curl is to add them on the command line like:
519*6236dae4SAndroid Build Coastguard Worker
520*6236dae4SAndroid Build Coastguard Worker    curl --cookie "name=Daniel" http://www.example.com
521*6236dae4SAndroid Build Coastguard Worker
522*6236dae4SAndroid Build Coastguard Worker Cookies are sent as common HTTP headers. This is practical as it allows curl
523*6236dae4SAndroid Build Coastguard Worker to record cookies simply by recording headers. Record cookies with curl by
524*6236dae4SAndroid Build Coastguard Worker using the [`--dump-header`](https://curl.se/docs/manpage.html#-D) (`-D`)
525*6236dae4SAndroid Build Coastguard Worker option like:
526*6236dae4SAndroid Build Coastguard Worker
527*6236dae4SAndroid Build Coastguard Worker    curl --dump-header headers_and_cookies http://www.example.com
528*6236dae4SAndroid Build Coastguard Worker
529*6236dae4SAndroid Build Coastguard Worker (Take note that the
530*6236dae4SAndroid Build Coastguard Worker [`--cookie-jar`](https://curl.se/docs/manpage.html#-c) option described
531*6236dae4SAndroid Build Coastguard Worker below is a better way to store cookies.)
532*6236dae4SAndroid Build Coastguard Worker
533*6236dae4SAndroid Build Coastguard Worker Curl has a full blown cookie parsing engine built-in that comes in use if you
534*6236dae4SAndroid Build Coastguard Worker want to reconnect to a server and use cookies that were stored from a
535*6236dae4SAndroid Build Coastguard Worker previous connection (or hand-crafted manually to fool the server into
536*6236dae4SAndroid Build Coastguard Worker believing you had a previous connection). To use previously stored cookies,
537*6236dae4SAndroid Build Coastguard Worker you run curl like:
538*6236dae4SAndroid Build Coastguard Worker
539*6236dae4SAndroid Build Coastguard Worker    curl --cookie stored_cookies_in_file http://www.example.com
540*6236dae4SAndroid Build Coastguard Worker
541*6236dae4SAndroid Build Coastguard Worker Curl's "cookie engine" gets enabled when you use the
542*6236dae4SAndroid Build Coastguard Worker [`--cookie`](https://curl.se/docs/manpage.html#-b) option. If you only
543*6236dae4SAndroid Build Coastguard Worker want curl to understand received cookies, use `--cookie` with a file that
544*6236dae4SAndroid Build Coastguard Worker does not exist. Example, if you want to let curl understand cookies from a
545*6236dae4SAndroid Build Coastguard Worker page and follow a location (and thus possibly send back cookies it received),
546*6236dae4SAndroid Build Coastguard Worker you can invoke it like:
547*6236dae4SAndroid Build Coastguard Worker
548*6236dae4SAndroid Build Coastguard Worker    curl --cookie nada --location http://www.example.com
549*6236dae4SAndroid Build Coastguard Worker
550*6236dae4SAndroid Build Coastguard Worker Curl has the ability to read and write cookie files that use the same file
551*6236dae4SAndroid Build Coastguard Worker format that Netscape and Mozilla once used. It is a convenient way to share
552*6236dae4SAndroid Build Coastguard Worker cookies between scripts or invokes. The `--cookie` (`-b`) switch
553*6236dae4SAndroid Build Coastguard Worker automatically detects if a given file is such a cookie file and parses it,
554*6236dae4SAndroid Build Coastguard Worker and by using the `--cookie-jar` (`-c`) option you make curl write a new
555*6236dae4SAndroid Build Coastguard Worker cookie file at the end of an operation:
556*6236dae4SAndroid Build Coastguard Worker
557*6236dae4SAndroid Build Coastguard Worker    curl --cookie cookies.txt --cookie-jar newcookies.txt \
558*6236dae4SAndroid Build Coastguard Worker      http://www.example.com
559*6236dae4SAndroid Build Coastguard Worker
560*6236dae4SAndroid Build Coastguard Worker# HTTPS
561*6236dae4SAndroid Build Coastguard Worker
562*6236dae4SAndroid Build Coastguard Worker## HTTPS is HTTP secure
563*6236dae4SAndroid Build Coastguard Worker
564*6236dae4SAndroid Build Coastguard Worker There are a few ways to do secure HTTP transfers. By far the most common
565*6236dae4SAndroid Build Coastguard Worker protocol for doing this is what is generally known as HTTPS, HTTP over
566*6236dae4SAndroid Build Coastguard Worker SSL. SSL encrypts all the data that is sent and received over the network and
567*6236dae4SAndroid Build Coastguard Worker thus makes it harder for attackers to spy on sensitive information.
568*6236dae4SAndroid Build Coastguard Worker
569*6236dae4SAndroid Build Coastguard Worker SSL (or TLS as the current version of the standard is called) offers a set of
570*6236dae4SAndroid Build Coastguard Worker advanced features to do secure transfers over HTTP.
571*6236dae4SAndroid Build Coastguard Worker
572*6236dae4SAndroid Build Coastguard Worker Curl supports encrypted fetches when built to use a TLS library and it can be
573*6236dae4SAndroid Build Coastguard Worker built to use one out of a fairly large set of libraries - `curl -V` shows
574*6236dae4SAndroid Build Coastguard Worker which one your curl was built to use (if any). To get a page from an HTTPS
575*6236dae4SAndroid Build Coastguard Worker server, simply run curl like:
576*6236dae4SAndroid Build Coastguard Worker
577*6236dae4SAndroid Build Coastguard Worker    curl https://secure.example.com
578*6236dae4SAndroid Build Coastguard Worker
579*6236dae4SAndroid Build Coastguard Worker## Certificates
580*6236dae4SAndroid Build Coastguard Worker
581*6236dae4SAndroid Build Coastguard Worker In the HTTPS world, you use certificates to validate that you are the one you
582*6236dae4SAndroid Build Coastguard Worker claim to be, as an addition to normal passwords. Curl supports client- side
583*6236dae4SAndroid Build Coastguard Worker certificates. All certificates are locked with a passphrase, which you need
584*6236dae4SAndroid Build Coastguard Worker to enter before the certificate can be used by curl. The passphrase can be
585*6236dae4SAndroid Build Coastguard Worker specified on the command line or if not, entered interactively when curl
586*6236dae4SAndroid Build Coastguard Worker queries for it. Use a certificate with curl on an HTTPS server like:
587*6236dae4SAndroid Build Coastguard Worker
588*6236dae4SAndroid Build Coastguard Worker    curl --cert mycert.pem https://secure.example.com
589*6236dae4SAndroid Build Coastguard Worker
590*6236dae4SAndroid Build Coastguard Worker curl also tries to verify that the server is who it claims to be, by
591*6236dae4SAndroid Build Coastguard Worker verifying the server's certificate against a locally stored CA cert bundle.
592*6236dae4SAndroid Build Coastguard Worker Failing the verification causes curl to deny the connection. You must then
593*6236dae4SAndroid Build Coastguard Worker use [`--insecure`](https://curl.se/docs/manpage.html#-k) (`-k`) in case you
594*6236dae4SAndroid Build Coastguard Worker want to tell curl to ignore that the server cannot be verified.
595*6236dae4SAndroid Build Coastguard Worker
596*6236dae4SAndroid Build Coastguard Worker More about server certificate verification and ca cert bundles can be read in
597*6236dae4SAndroid Build Coastguard Worker the [`SSLCERTS` document](https://curl.se/docs/sslcerts.html).
598*6236dae4SAndroid Build Coastguard Worker
599*6236dae4SAndroid Build Coastguard Worker At times you may end up with your own CA cert store and then you can tell
600*6236dae4SAndroid Build Coastguard Worker curl to use that to verify the server's certificate:
601*6236dae4SAndroid Build Coastguard Worker
602*6236dae4SAndroid Build Coastguard Worker    curl --cacert ca-bundle.pem https://example.com/
603*6236dae4SAndroid Build Coastguard Worker
604*6236dae4SAndroid Build Coastguard Worker# Custom Request Elements
605*6236dae4SAndroid Build Coastguard Worker
606*6236dae4SAndroid Build Coastguard Worker## Modify method and headers
607*6236dae4SAndroid Build Coastguard Worker
608*6236dae4SAndroid Build Coastguard Worker Doing fancy stuff, you may need to add or change elements of a single curl
609*6236dae4SAndroid Build Coastguard Worker request.
610*6236dae4SAndroid Build Coastguard Worker
611*6236dae4SAndroid Build Coastguard Worker For example, you can change the POST method to `PROPFIND` and send the data
612*6236dae4SAndroid Build Coastguard Worker as `Content-Type: text/xml` (instead of the default `Content-Type`) like
613*6236dae4SAndroid Build Coastguard Worker this:
614*6236dae4SAndroid Build Coastguard Worker
615*6236dae4SAndroid Build Coastguard Worker    curl --data "<xml>" --header "Content-Type: text/xml" \
616*6236dae4SAndroid Build Coastguard Worker      --request PROPFIND example.com
617*6236dae4SAndroid Build Coastguard Worker
618*6236dae4SAndroid Build Coastguard Worker You can delete a default header by providing one without content. Like you
619*6236dae4SAndroid Build Coastguard Worker can ruin the request by chopping off the `Host:` header:
620*6236dae4SAndroid Build Coastguard Worker
621*6236dae4SAndroid Build Coastguard Worker    curl --header "Host:" http://www.example.com
622*6236dae4SAndroid Build Coastguard Worker
623*6236dae4SAndroid Build Coastguard Worker You can add headers the same way. Your server may want a `Destination:`
624*6236dae4SAndroid Build Coastguard Worker header, and you can add it:
625*6236dae4SAndroid Build Coastguard Worker
626*6236dae4SAndroid Build Coastguard Worker    curl --header "Destination: http://nowhere" http://example.com
627*6236dae4SAndroid Build Coastguard Worker
628*6236dae4SAndroid Build Coastguard Worker## More on changed methods
629*6236dae4SAndroid Build Coastguard Worker
630*6236dae4SAndroid Build Coastguard Worker It should be noted that curl selects which methods to use on its own
631*6236dae4SAndroid Build Coastguard Worker depending on what action to ask for. `-d` makes a POST, `-I` makes a HEAD and
632*6236dae4SAndroid Build Coastguard Worker so on. If you use the [`--request`](https://curl.se/docs/manpage.html#-X) /
633*6236dae4SAndroid Build Coastguard Worker `-X` option you can change the method keyword curl selects, but you do not
634*6236dae4SAndroid Build Coastguard Worker modify curl's behavior. This means that if you for example use -d "data" to
635*6236dae4SAndroid Build Coastguard Worker do a POST, you can modify the method to a `PROPFIND` with `-X` and curl still
636*6236dae4SAndroid Build Coastguard Worker thinks it sends a POST. You can change the normal GET to a POST method by
637*6236dae4SAndroid Build Coastguard Worker simply adding `-X POST` in a command line like:
638*6236dae4SAndroid Build Coastguard Worker
639*6236dae4SAndroid Build Coastguard Worker    curl -X POST http://example.org/
640*6236dae4SAndroid Build Coastguard Worker
641*6236dae4SAndroid Build Coastguard Worker curl however still acts as if it sent a GET so it does not send any request
642*6236dae4SAndroid Build Coastguard Worker body etc.
643*6236dae4SAndroid Build Coastguard Worker
644*6236dae4SAndroid Build Coastguard Worker# Web Login
645*6236dae4SAndroid Build Coastguard Worker
646*6236dae4SAndroid Build Coastguard Worker## Some login tricks
647*6236dae4SAndroid Build Coastguard Worker
648*6236dae4SAndroid Build Coastguard Worker While not strictly just HTTP related, it still causes a lot of people
649*6236dae4SAndroid Build Coastguard Worker problems so here's the executive run-down of how the vast majority of all
650*6236dae4SAndroid Build Coastguard Worker login forms work and how to login to them using curl.
651*6236dae4SAndroid Build Coastguard Worker
652*6236dae4SAndroid Build Coastguard Worker It can also be noted that to do this properly in an automated fashion, you
653*6236dae4SAndroid Build Coastguard Worker most certainly need to script things and do multiple curl invokes etc.
654*6236dae4SAndroid Build Coastguard Worker
655*6236dae4SAndroid Build Coastguard Worker First, servers mostly use cookies to track the logged-in status of the
656*6236dae4SAndroid Build Coastguard Worker client, so you need to capture the cookies you receive in the responses.
657*6236dae4SAndroid Build Coastguard Worker Then, many sites also set a special cookie on the login page (to make sure
658*6236dae4SAndroid Build Coastguard Worker you got there through their login page) so you should make a habit of first
659*6236dae4SAndroid Build Coastguard Worker getting the login-form page to capture the cookies set there.
660*6236dae4SAndroid Build Coastguard Worker
661*6236dae4SAndroid Build Coastguard Worker Some web-based login systems feature various amounts of JavaScript, and
662*6236dae4SAndroid Build Coastguard Worker sometimes they use such code to set or modify cookie contents. Possibly they
663*6236dae4SAndroid Build Coastguard Worker do that to prevent programmed logins, like this manual describes how to...
664*6236dae4SAndroid Build Coastguard Worker Anyway, if reading the code is not enough to let you repeat the behavior
665*6236dae4SAndroid Build Coastguard Worker manually, capturing the HTTP requests done by your browsers and analyzing the
666*6236dae4SAndroid Build Coastguard Worker sent cookies is usually a working method to work out how to shortcut the
667*6236dae4SAndroid Build Coastguard Worker JavaScript need.
668*6236dae4SAndroid Build Coastguard Worker
669*6236dae4SAndroid Build Coastguard Worker In the actual `<form>` tag for the login, lots of sites fill-in
670*6236dae4SAndroid Build Coastguard Worker random/session or otherwise secretly generated hidden tags and you may need
671*6236dae4SAndroid Build Coastguard Worker to first capture the HTML code for the login form and extract all the hidden
672*6236dae4SAndroid Build Coastguard Worker fields to be able to do a proper login POST. Remember that the contents need
673*6236dae4SAndroid Build Coastguard Worker to be URL encoded when sent in a normal POST.
674*6236dae4SAndroid Build Coastguard Worker
675*6236dae4SAndroid Build Coastguard Worker# Debug
676*6236dae4SAndroid Build Coastguard Worker
677*6236dae4SAndroid Build Coastguard Worker## Some debug tricks
678*6236dae4SAndroid Build Coastguard Worker
679*6236dae4SAndroid Build Coastguard Worker Many times when you run curl on a site, you notice that the site does not
680*6236dae4SAndroid Build Coastguard Worker seem to respond the same way to your curl requests as it does to your
681*6236dae4SAndroid Build Coastguard Worker browser's.
682*6236dae4SAndroid Build Coastguard Worker
683*6236dae4SAndroid Build Coastguard Worker Then you need to start making your curl requests more similar to your
684*6236dae4SAndroid Build Coastguard Worker browser's requests:
685*6236dae4SAndroid Build Coastguard Worker
686*6236dae4SAndroid Build Coastguard Worker - Use the `--trace-ascii` option to store fully detailed logs of the requests
687*6236dae4SAndroid Build Coastguard Worker   for easier analyzing and better understanding
688*6236dae4SAndroid Build Coastguard Worker
689*6236dae4SAndroid Build Coastguard Worker - Make sure you check for and use cookies when needed (both reading with
690*6236dae4SAndroid Build Coastguard Worker   `--cookie` and writing with `--cookie-jar`)
691*6236dae4SAndroid Build Coastguard Worker
692*6236dae4SAndroid Build Coastguard Worker - Set user-agent (with [`-A`](https://curl.se/docs/manpage.html#-A)) to
693*6236dae4SAndroid Build Coastguard Worker   one like a recent popular browser does
694*6236dae4SAndroid Build Coastguard Worker
695*6236dae4SAndroid Build Coastguard Worker - Set referer (with [`-E`](https://curl.se/docs/manpage.html#-E)) like
696*6236dae4SAndroid Build Coastguard Worker   it is set by the browser
697*6236dae4SAndroid Build Coastguard Worker
698*6236dae4SAndroid Build Coastguard Worker - If you use POST, make sure you send all the fields and in the same order as
699*6236dae4SAndroid Build Coastguard Worker   the browser does it.
700*6236dae4SAndroid Build Coastguard Worker
701*6236dae4SAndroid Build Coastguard Worker## Check what the browsers do
702*6236dae4SAndroid Build Coastguard Worker
703*6236dae4SAndroid Build Coastguard Worker A good helper to make sure you do this right, is the web browsers' developers
704*6236dae4SAndroid Build Coastguard Worker tools that let you view all headers you send and receive (even when using
705*6236dae4SAndroid Build Coastguard Worker HTTPS).
706*6236dae4SAndroid Build Coastguard Worker
707*6236dae4SAndroid Build Coastguard Worker A more raw approach is to capture the HTTP traffic on the network with tools
708*6236dae4SAndroid Build Coastguard Worker such as Wireshark or tcpdump and check what headers that were sent and
709*6236dae4SAndroid Build Coastguard Worker received by the browser. (HTTPS forces you to use `SSLKEYLOGFILE` to do
710*6236dae4SAndroid Build Coastguard Worker that.)
711