1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
7
<year>2004</year><year>2010</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
11
The contents of this file are subject to the Erlang Public License,
12
Version 1.1, (the "License"); you may not use this file except in
13
compliance with the License. You should have received a copy of the
14
Erlang Public License along with this software. If not, it can be
15
retrieved online at http://www.erlang.org/.
17
Software distributed under the License is distributed on an "AS IS"
18
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
19
the License for the specific language governing rights and limitations
25
<prepared>Ingela Anderton Andin</prepared>
26
<responsible></responsible>
31
<module>httpc</module>
32
<modulesummary>An HTTP/1.1 client </modulesummary>
34
<p>This module provides the API to a HTTP/1.1 compatible client according
35
to RFC 2616, caching is currently not supported.</p>
37
<p>When starting the Inets application a manager process for the
38
default profile will be started. The functions in this API
39
that does not explicitly use a profile will accesses the
40
default profile. A profile keeps track of proxy options,
41
cookies and other options that can be applied to more than one
45
https is used the ssl application needs to be started.</p>
47
<p>Also note that pipelining will only be used if the pipeline
48
timeout is set, otherwise persistent connections without
49
pipelining will be used e.i. the client always waits for
50
the previous response before sending the next request.</p>
52
<p>There are some usage examples in the <seealso
53
marker="http_client">Inets User's Guide.</seealso></p>
57
<title>COMMON DATA TYPES </title>
58
<p>Type definitions that are used more than once in
60
<code type="none"><![CDATA[
61
boolean() = true | false
62
string() = list of ASCII characters
65
path() = string() representing a file path or directory path
66
ip_address() = See inet(3)
67
socket_opt() = See the Options used by gen_tcp(3) and
74
<title>HTTP DATA TYPES </title>
75
<p>Type definitions that are related to HTTP:</p>
76
<p>For more information about HTTP see rfc 2616</p>
78
<code type="none"><![CDATA[
79
method() = head | get | put | post | trace | options | delete
80
request() = {url(), headers()} |
81
{url(), headers(), content_type(), body()}
82
url() = string() - Syntax according to the URI definition in rfc 2396, ex: "http://www.erlang.org"
83
status_line() = {http_version(), status_code(), reason_phrase()}
84
http_version() = string() ex: "HTTP/1.1"
85
status_code() = integer()
86
reason_phrase() = string()
87
content_type() = string()
88
headers() = [header()]
89
header() = {field(), value()}
92
body() = string() | binary()
99
<title>SSL DATA TYPES </title>
100
<p>Some type definitions relevant when using https,
101
for details <seealso marker="ssl:ssl">ssl(3)</seealso>: </p>
102
<code type="none"><![CDATA[
103
ssl_options() = {verify, code()} |
107
{password, string()} |
108
{cacertfile, path()} |
114
<title>HTTP CLIENT SERVICE START/STOP </title>
116
<p>A HTTP client can be configured to start when starting the inets
117
application or started dynamically in runtime by calling the
118
inets application API <c>inets:start(httpc, ServiceConfig)</c>, or
119
<c>inets:start(httpc, ServiceConfig, How)</c>
120
see <seealso marker="inets">inets(3)</seealso> Below follows a
121
description of the available configuration options.</p>
123
<tag>{profile, profile()}</tag>
124
<item>Name of the profile, see
125
common data types below, this option is mandatory.</item>
126
<tag>{data_dir, path()}</tag>
127
<item>Directory where the profile
128
may save persistent data, if omitted all cookies will be treated
129
as session cookies.</item>
132
<p>The client can be stopped using inets:stop(httpc, Pid) or
133
inets:stop(httpc, Profile).</p>
135
<marker id="request1"></marker>
140
<name>request(Url) -> </name>
141
<name>request(Url, Profile) -> {ok, Result} | {error, Reason}</name>
142
<fsummary>Sends a get HTTP-request</fsummary>
145
<v>Result = {status_line(), headers(), body()} |
146
{status_code(), body()} | request_id() </v>
147
<v>Profile = profile()</v>
148
<v>Reason = term() </v>
151
<p>Equivalent to httpc:request(get, {Url, []}, [], []).</p>
153
<marker id="request2"></marker>
158
<name>request(Method, Request, HTTPOptions, Options) -> </name>
159
<name>request(Method, Request, HTTPOptions, Options, Profile) -> {ok, Result} | {ok, saved_to_file} | {error, Reason}</name>
161
<fsummary>Sends a HTTP-request</fsummary>
163
<v>Method = method() </v>
164
<v>Request = request()</v>
165
<v>HTTPOptions = http_options()</v>
166
<v>http_options() = [http_option()]</v>
167
<v>http_option() = {timeout, timeout()} |
168
{connect_timeout, timeout()} |
169
{ssl, ssl_options()} |
170
{autoredirect, boolean()} |
171
{proxy_auth, {userstring(), passwordstring()}} |
172
{version, http_version()} |
173
{relaxed, boolean()}</v>
174
<v>timeout() = integer() >= 0 | infinity</v>
175
<v>Options = options()</v>
176
<v>options() = [option()]</v>
177
<v>option() = {sync, boolean()} |
178
{stream, stream_to()} |
179
{body_format, body_format()} |
180
{full_result, boolean()} |
181
{headers_as_is, boolean() |
182
{socket_opts, socket_opts()} |
183
{receiver, receiver()}}</v>
184
<v>stream_to() = none | self | {self, once} | filename() </v>
185
<v>socket_opts() = [socket_opt()]</v>
186
<v>receiver() = pid() | function()/1 | {Module, Function, Args} </v>
187
<v>Module = atom() </v>
188
<v>Function = atom() </v>
189
<v>Args = list() </v>
190
<v>body_format() = string | binary </v>
191
<v>Result = {status_line(), headers(), body()} |
192
{status_code(), body()} | request_id() </v>
193
<v>Profile = profile() </v>
194
<v>Reason = term() </v>
198
<p>Sends a HTTP-request. The function can be both synchronous
199
and asynchronous. In the later case the function will return
200
{ok, RequestId} and later on the information will be delivered
201
to the <c>receiver</c> depending on that value. </p>
203
<p>Http option (<c>http_option()</c>) details: </p>
205
<tag><c><![CDATA[timeout]]></c></tag>
207
<p>Timeout time for the request. </p>
208
<p>The clock start ticking as soon as the request has been
210
<p>Time is in milliseconds. </p>
211
<p>Defaults to <c>infinity</c>. </p>
214
<tag><c><![CDATA[connect_timeout]]></c></tag>
216
<p>Connection timeout time, used during the initial request,
217
when the client is <em>connecting</em> to the server. </p>
218
<p>Time is in milliseconds. </p>
219
<p>Defaults to the value of the <c>timeout</c> option. </p>
222
<tag><c><![CDATA[ssl]]></c></tag>
224
<p>If using SSL, these SSL-specific options are used. </p>
225
<p>Defaults to <c>[]</c>. </p>
228
<tag><c><![CDATA[autoredirect]]></c></tag>
230
<p>Should the client automatically retreive the information
231
from the new URI and return that as the result instead
232
of a 30X-result code. </p>
233
<p>Note that for some 30X-result codes automatic redirect
234
is not allowed in these cases the 30X-result will always
236
<p>Defaults to <c>true</c>. </p>
239
<tag><c><![CDATA[proxy_auth]]></c></tag>
241
<p>A proxy-authorization header using the provided user name and
242
password will be added to the request. </p>
245
<tag><c><![CDATA[version]]></c></tag>
247
<p>Can be used to make the client act as an <c>HTTP/1.0</c> or
248
<c>HTTP/0.9</c> client. By default this is an <c>HTTP/1.1</c>
249
client. When using <c>HTTP/1.0</c> persistent connections will
251
<p>Defaults to the trsing <c>"HTTP/1.1"</c>. </p>
254
<tag><c><![CDATA[relaxed]]></c></tag>
256
<p>If set to true workarounds for known server deviations from
257
the HTTP-standard are enabled. </p>
258
<p>Defaults to <c>false</c>. </p>
263
<p>Option (<c>option()</c>) details: </p>
265
<tag><c><![CDATA[sync]]></c></tag>
267
<p>Shall the request be synchronous or asynchronous. </p>
268
<p>Defaults to <c>true</c>. </p>
271
<tag><c><![CDATA[stream]]></c></tag>
273
<p>Streams the body of a 200 or 206 response to the calling
274
process or to a file. When streaming to the calling process
275
using the option <c>self</c> the the following stream messages
276
will be sent to that process: {http, {RequestId,
277
stream_start, Headers}, {http, {RequestId, stream,
278
BinBodyPart}, {http, {RequestId, stream_end, Headers}. When
279
streaming to to the calling processes using the option
280
<c>{self, once}</c> the first message will have an additional
281
element e.i. {http, {RequestId, stream_start, Headers, Pid},
282
this is the process id that should be used as an argument to
283
http:stream_next/1 to trigger the next message to be sent to
284
the calling process. </p>
285
<p>Note that it is possible that chunked encoding will add
286
headers so that there are more headers in the stream_end
287
message than in the stream_start.
288
When streaming to a file and the request is asynchronous the
289
message {http, {RequestId, saved_to_file}} will be sent. </p>
290
<p>Defaults to <c>none</c>. </p>
293
<tag><c><![CDATA[body_format]]></c></tag>
295
<p>Defines if the body shall be delivered as a string or as a
296
binary. This option is only valid for the synchronous
298
<p>Defaults to <c>string</c>. </p>
301
<tag><c><![CDATA[full_result]]></c></tag>
303
<p>Should a "full result" be returned to the caller (that is,
304
the body, the headers and the entire status-line) or not
305
(the body and the status code). </p>
306
<p>Defaults to <c>true</c>. </p>
309
<tag><c><![CDATA[header_as_is]]></c></tag>
311
<p>Shall the headers provided by the user be made
312
lower case or be regarded as case sensitive. </p>
313
<p>Note that the http standard requires them to be
314
case insenstive. This feature should only be used if there is
315
no other way to communicate with the server or for testing
316
purpose. Also note that when this option is used no headers
317
will be automatically added, all necessary headers has to be
318
provided by the user. </p>
319
<p>Defaults to <c>false</c>. </p>
322
<tag><c><![CDATA[socket_opts]]></c></tag>
324
<p>Socket options to be used for this and subsequent
326
<p>Overrides any value set by the
327
<seealso marker="set_options">set_options</seealso>
329
<p>Note that the validity of the options are <em>not</em>
330
checked in any way. </p>
331
<p>Note that this may change the socket behaviour
332
(see <seealso marker="kernel:inet#setopts">inet:setopts/2</seealso>)
333
for an already existing, and therefor already connected
334
request handler. </p>
335
<p>By defaults the socket options set by the
336
<seealso marker="#set_options">set_options/1,2</seealso>
337
function is used when establishing connection. </p>
340
<tag><c><![CDATA[receiver]]></c></tag>
342
<p>Defines how the client will deliver the result for a
343
asynchroneous request (<c>sync</c> has the value
347
<tag><c><![CDATA[pid()]]></c></tag>
349
<p>Message(s) will be sent to this process in the format: </p>
355
<tag><c><![CDATA[function/1]]></c></tag>
357
<p>Information will be delivered to the receiver via calls
358
to the provided fun: </p>
364
<tag><c><![CDATA[{Module, Funcion, Args}]]></c></tag>
366
<p>Information will be delivered to the receiver via calls
367
to the callback function: </p>
369
apply(Module, Function, [ReplyInfo | Args])
374
<p>In all cases above, <c>ReplyInfo</c> has the following
378
{RequestId, saved_to_file}
379
{RequestId, {error, Reason}}
381
{RequestId, stream_start, Headers}
382
{RequestId, stream_start, Headers, HandlerPid}
383
{RequestId, stream, BinBodyPart}
384
{RequestId, stream_end, Headers}
387
<p>Defaults to the <c>pid()</c> of the process calling the request
388
function (<c>self()</c>). </p>
392
<marker id="cancel_request"></marker>
397
<name>cancel_request(RequestId) -> </name>
398
<name>cancel_request(RequestId, Profile) -> ok</name>
399
<fsummary>Cancels an asynchronous HTTP-request.</fsummary>
401
<v>RequestId = request_id() - A unique identifier as returned
403
<v>Profile = profile()</v>
406
<p>Cancels an asynchronous HTTP-request. </p>
408
<marker id="set_options"></marker>
413
<name>set_options(Options) -> </name>
414
<name>set_options(Options, Profile) -> ok | {error, Reason}</name>
415
<fsummary>Sets options to be used for subsequent requests.</fsummary>
417
<v>Options = [Option]</v>
418
<v>Option = {proxy, {Proxy, NoProxy}} |
419
{max_sessions, MaxSessions} |
420
{max_keep_alive_length, MaxKeepAlive} |
421
{keep_alive_timeout, KeepAliveTimeout} |
422
{max_pipeline_length, MaxPipeline} |
423
{pipeline_timeout, PipelineTimeout} |
424
{cookies, CookieMode} |
425
{ipfamily, IpFamily} |
428
{socket_opts, socket_opts()} |
429
{verbose, VerboseMode} </v>
430
<v>Proxy = {Hostname, Port}</v>
431
<v>Hostname = string() </v>
432
<d>ex: "localhost" or "foo.bar.se"</d>
433
<v>Port = integer()</v>
435
<v>socket_opts() = [socket_opt()]</v>
436
<d>The options are appended to the socket options used by the
438
<d>These are the default values when a new request handler
439
is started (for the initial connect). They are passed directly
440
to the underlying transport (gen_tcp or ssl) <em>without</em>
442
<v>NoProxy = [NoProxyDesc]</v>
443
<v>NoProxyDesc = DomainDesc | HostName | IPDesc</v>
444
<v>DomainDesc = "*.Domain"</v>
445
<d>ex: "*.ericsson.se"</d>
446
<v>IpDesc = string()</v>
447
<d>ex: "134.138" or "[FEDC:BA98" (all IP-addresses starting with 134.138 or FEDC:BA98), "66.35.250.150" or "[2010:836B:4179::836B:4179]" (a complete IP-address).</d>
448
<v>MaxSessions = integer() </v>
449
<d>Default is <em>2</em>.
450
Maximum number of persistent connections to a host.</d>
451
<v>MaxKeepAlive = integer() </v>
452
<d>Default is <em>5</em>.
453
Maximum number of outstanding requests on the same connection to
455
<v>KeepAliveTimeout = integer() </v>
456
<d>Default is <em>120000</em> (= 2 min).
457
If a persistent connection is idle longer than the
458
keep_alive_timeout the client will close the connection.
459
The server may also have a such a time out but you should
461
<v>MaxPipeline = integer() </v>
462
<d>Default is <em>2</em>.
463
Maximum number of outstanding requests on a pipelined connection to a host.</d>
464
<v>PipelineTimeout = integer() </v>
465
<d>Default is <em>0</em>,
466
which will result in pipelining not being used.
467
If a persistent connection is idle longer than the
468
pipeline_timeout the client will close the connection. </d>
469
<v>CookieMode = enabled | disabled | verify </v>
470
<d>Default is <em>disabled</em>.
471
If Cookies are enabled all valid cookies will automatically be
472
saved in the client manager's cookie database.
473
If the option verify is used the function http:verify_cookie/2
474
has to be called for the cookie to be saved.</d>
475
<v>IpFamily = inet | inet6 | inet6fb4 </v>
476
<d>By default <em>inet</em>.
477
When it is set to <c>inet6fb4</c> you can use both ipv4 and ipv6.
478
It first tries <c>inet6</c> and if that does not works falls back to <c>inet</c>.
479
The option is here to provide a workaround for buggy ipv6 stacks to ensure that
480
ipv4 will always work.</d>
481
<v>IpAddress = ip_address() </v>
482
<d>If the host has several network interfaces, this option specifies which one to use.
483
See gen_tcp:connect/3,4 for more info. </d>
484
<v>Port = integer() </v>
485
<d>Specify which local port number to use.
486
See gen_tcp:connect/3,4 for more info. </d>
487
<v>VerboseMode = false | verbose | debug | trace </v>
488
<d>Default is <em>false</em>.
489
This option is used to switch on (or off)
490
different levels of erlang trace on the client.
491
It is a debug feature.</d>
492
<v>Profile = profile()</v>
495
<p>Sets options to be used for subsequent
498
<p>If possible the client will keep its connections
499
alive and use persistent connections
500
with or without pipeline depending on configuration
501
and current circumstances. The HTTP/1.1 specification does not
502
provide a guideline for how many requests that would be
503
ideal to be sent on a persistent connection,
504
this very much depends on the
505
application. Note that a very long queue of requests may cause a
506
user perceived delays as earlier request may take a long time
507
to complete. The HTTP/1.1 specification does suggest a
508
limit of 2 persistent connections per server, which is the
509
default value of the max_sessions option. </p>
512
<marker id="stream_next"></marker>
517
<name>stream_next(Pid) -> ok</name>
518
<fsummary> Triggers the next message to be streamed, e.i.
519
same behavior as active once for sockets.
522
<v>Pid = pid() - as received in the stream_start message</v>
525
<p>Triggers the next message to be streamed, e.i.
526
same behavior as active once for sockets.</p>
528
<marker id="verify_cookie"></marker>
529
<marker id="store_cookie"></marker>
534
<name>store_cookie(SetCookieHeaders, Url) -> </name>
535
<name>store_cookie(SetCookieHeaders, Url, Profile) -> ok | {error, Reason}</name>
536
<fsummary>Saves the cookies defined in SetCookieHeaders in the client profile's cookie database.</fsummary>
538
<v>SetCookieHeaders = headers() - where field = "set-cookie"</v>
540
<v>Profile = profile()</v>
543
<p>Saves the cookies defined in SetCookieHeaders
544
in the client profile's cookie database. You need to
545
call this function if you set the option cookies to <c>verify</c>.
546
If no profile is specified the default profile will be used.
549
<marker id="cookie_header"></marker>
554
<name>cookie_header(Url) -> </name>
555
<name>cookie_header(Url, Profile) -> header() | {error, Rason}</name>
556
<fsummary>Returns the cookie header that would be sent when
557
making a request to Url using the profile Profile.</fsummary>
560
<v>Profile = profile()</v>
563
<p>Returns the cookie header that would be sent
564
when making a request to Url using the profile Profile.
565
If no profile is specified the default profile will be used.
568
<marker id="reset_cookies"></marker>
574
<name>reset_cookies() -> void()</name>
575
<name>reset_cookies(Profile) -> void()</name>
576
<fsummary>Reset the cookie database.</fsummary>
578
<v>Profile = profile()</v>
581
<p>Resets (clears) the cookie database for the specified Profile.
582
If no profile is specified the default profile will be used.
589
<name>which_cookies() -> cookies()</name>
590
<name>which_cookies(Profile) -> cookies()</name>
591
<fsummary>Dumps out the entire cookie database.</fsummary>
593
<v>Profile = profile()</v>
594
<v>cookies() = [cooie_stores()]</v>
595
<v>cookie_stores() = {cookies, icookies()} | {session_cookies, icookies()}</v>
596
<v>icookies() = [icookie()]</v>
597
<v>cookie() = term()</v>
600
<p>This function produces a list of the entire cookie database.
601
It is intended for debugging/testing purposes.
602
If no profile is specified the default profile will be used.
609
<title>SEE ALSO</title>
610
<p>RFC 2616, <seealso marker="inets">inets(3)</seealso>,
611
<seealso marker="kernel:gen_tcp">gen_tcp(3)</seealso>,
612
<seealso marker="ssl:ssl">ssl(3)</seealso>