1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
7
<year>2004</year><year>2009</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>
32
<modulesummary>An HTTP/1.1 client </modulesummary>
34
<p>This module provides the API to a HTTP/1.1 client according to
35
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)
72
<title>HTTP DATA TYPES </title>
73
<p>Type definitions that are related to HTTP:</p>
74
<p>For more information about HTTP see rfc 2616</p>
76
<code type="none"><![CDATA[
77
method() = head | get | put | post | trace | options | delete
78
request() = {url(), headers()} |
79
{url(), headers(), content_type(), body()}
80
url() = string() - Syntax according to the URI definition in rfc 2396, ex: "http://www.erlang.org"
81
status_line() = {http_version(), status_code(), reason_phrase()}
82
http_version() = string() ex: "HTTP/1.1"
83
status_code() = integer()
84
reason_phrase() = string()
85
content_type() = string()
86
headers() = [header()]
87
header() = {field(), value()}
90
body() = string() | binary()
97
<title>SSL DATA TYPES </title>
98
<p>Some type definitions relevant when using https,
99
for details <seealso marker="ssl:ssl">ssl(3)</seealso>: </p>
100
<code type="none"><![CDATA[
101
ssl_options() = {verify, code()} |
105
{password, string()} |
106
{cacertfile, path()} |
112
<title>HTTP CLIENT SERVICE START/STOP </title>
114
<p>A HTTP client can be configured to start when starting the inets
115
application or started dynamically in runtime by calling the
116
inets application API <c>inets:start(httpc, ServiceConfig)</c>, or
117
<c>inets:start(httpc, ServiceConfig, How)</c>
118
see <seealso marker="inets">inets(3)</seealso> Below follows a
119
description of the available configuration options.</p>
121
<tag>{profile, profile()}</tag>
122
<item>Name of the profile, see
123
common data types below, this option is mandatory.</item>
124
<tag>{data_dir, path()}</tag>
125
<item>Directory where the profile
126
may save persistent data, if omitted all cookies will be treated
127
as session cookies.</item>
130
<p>The client can be stopped using inets:stop(httpc, Pid) or
131
inets:stop(httpc, Profile).</p>
133
<marker id="cancel_request"></marker>
138
<name>cancel_request(RequestId) -> </name>
139
<name>cancel_request(RequestId, Profile) -> ok</name>
140
<fsummary>Cancels an asynchronous HTTP-request.</fsummary>
142
<v>RequestId = request_id() - A unique identifier as returned
144
<v>Profile = profile()</v>
147
<p>Cancels an asynchronous HTTP-request. </p>
149
<marker id="request1"></marker>
154
<name>request(Url) -> </name>
155
<name>request(Url, Profile) -> {ok, Result} | {error, Reason}</name>
156
<fsummary>Sends a get HTTP-request</fsummary>
158
<v>Url = url() </v> <v>Result = {status_line(), headers(),
159
body()} | {status_code(), body()} | request_id() </v>
160
<v>Profile = profile()</v>
161
<v>Reason = term() </v>
164
<p>Equivalent to http:request(get, {Url, []}, [], []).</p>
166
<marker id="request2"></marker>
171
<name>request(Method, Request, HTTPOptions, Options) -> </name>
172
<name>request(Method, Request, HTTPOptions, Options, Profile) -> {ok, Result} | {ok, saved_to_file} | {error, Reason}</name>
174
<fsummary>Sends a HTTP-request</fsummary>
176
<v>Method = method() </v>
177
<v>Request = request()</v>
178
<v>HTTPOptions = http_options()</v>
179
<v>http_options() = [http_option()]</v>
180
<v>http_option() = {timeout, timeout()} |
181
{connect_timeout, timeout()} |
182
{ssl, ssl_options()} |
183
{autoredirect, boolean()} |
184
{proxy_auth, {userstring(), passwordstring()}} |
185
{version, http_version()} |
186
{relaxed, boolean()}</v>
187
<v>timeout() = integer() >= 0 | infinity</v>
188
<v>Options = options()</v>
189
<v>options() = [option()]</v>
190
<v>option() = {sync, boolean()} |
191
{stream, stream_to()} |
192
{body_format, body_format()} |
193
{full_result, boolean()} |
194
{headers_as_is, boolean()}</v>
195
<v>stream_to() = self | {self, once} | filename() </v>
196
<v>body_format() = string() | binary() </v>
197
<v>Result = {status_line(), headers(), body()} |
198
{status_code(), body()} | request_id() </v>
199
<v>Profile = profile() </v>
200
<v>Reason = term() </v>
204
<p>Sends a HTTP-request. The function can be both synchronous
205
and asynchronous. In the later case the function will return
206
{ok, RequestId} and later on message(s) will be sent to the
207
calling process on the format: </p>
209
{http, {RequestId, Result}}
210
{http, {RequestId, {error, Reason}}}
211
{http, {RequestId, stream_start, Headers}
212
{http, {RequestId, stream, BinBodyPart}
213
{http, {RequestId, stream_end, Headers}
214
{http, {RequestId, saved_to_file}}.
217
<p>Http option (<c>http_option()</c>) details: </p>
219
<tag><c><![CDATA[timeout]]></c></tag>
221
<p>Timeout time for the request. </p>
222
<p>Defaults to <c>infinity</c>. </p>
225
<tag><c><![CDATA[connect_timeout]]></c></tag>
227
<p>Connection timeout time, used during the initial request,
228
when the client is connecting to the server. </p>
229
<p>Defaults to the value of the <c>timeout</c> option. </p>
232
<tag><c><![CDATA[ssl]]></c></tag>
234
<p>If using SSL, these SSL-specific options are used. </p>
235
<p>Defaults to <c>[]</c>. </p>
238
<tag><c><![CDATA[autoredirect]]></c></tag>
240
<p>Should the client automatically retreive the information
241
from the new URI and return that as the result instead
242
of a 30X-result code. </p>
243
<p>Note that for some 30X-result codes automatic redirect
244
is not allowed in these cases the 30X-result will always
246
<p>Defaults to <c>true</c>. </p>
249
<tag><c><![CDATA[proxy_auth]]></c></tag>
251
<p>A proxy-authorization header using the provided user name and
252
password will be added to the request. </p>
255
<tag><c><![CDATA[version]]></c></tag>
257
<p>Can be used to make the client act as an <c>HTTP/1.0</c> or
258
<c>HTTP/0.9</c> client. By default this is an <c>HTTP/1.1</c>
259
client. When using <c>HTTP/1.0</c> persistent connections will
261
<p>Defaults to the trsing <c>"HTTP/1.1"</c>. </p>
264
<tag><c><![CDATA[relaxed]]></c></tag>
266
<p>If set to true workarounds for known server deviations from
267
the HTTP-standard are enabled. </p>
268
<p>Defaults to <c>false</c>. </p>
273
<p>Option (<c>option()</c>) details: </p>
275
<tag><c><![CDATA[sync]]></c></tag>
277
<p>Shall the request be synchronous or asynchronous. </p>
278
<p>Defaults to <c>true</c>. </p>
281
<tag><c><![CDATA[stream]]></c></tag>
283
<p>Streams the body of a 200 or 206 response to the calling
284
process or to a file. When streaming to the calling process
285
using the option <c>self</c> the the following stream messages
286
will be sent to that process: {http, {RequestId,
287
stream_start, Headers}, {http, {RequestId, stream,
288
BinBodyPart}, {http, {RequestId, stream_end, Headers}. When
289
streaming to to the calling processes using the option
290
<c>{self once}</c> the first message will have an additional
291
element e.i. {http, {RequestId, stream_start, Headers, Pid},
292
this is the process id that should be used as an argument to
293
http:stream_next/1 to trigger the next message to be sent to
294
the calling process. </p>
295
<p>Note that it is possible that chunked encoding will add
296
headers so that there are more headers in the stream_end
297
message than in the stream_start.
298
When streaming to a file and the request is asynchronous the
299
message {http, {RequestId, saved_to_file}} will be sent. </p>
300
<p>Defaults to <c>none</c>. </p>
303
<tag><c><![CDATA[body_format]]></c></tag>
305
<p>Defines if the body shall be delivered as a string or as a
306
binary. This option is only valid for the synchronous
308
<p>Defaults to <c>string</c>. </p>
311
<tag><c><![CDATA[full_result]]></c></tag>
313
<p>Should a "full result" be returned to the caller (that is,
314
the body, the headers and the entire status-line) or not
315
(the body and the status code). </p>
316
<p>Defaults to <c>true</c>. </p>
319
<tag><c><![CDATA[header_as_is]]></c></tag>
321
<p>Shall the headers provided by the user be made
322
lower case or be regarded as case sensitive. </p>
323
<p>Note that the http standard requires them to be
324
case insenstive. This feature should only be used if there is
325
no other way to communicate with the server or for testing
326
purpose. Also note that when this option is used no headers
327
will be automatically added, all necessary headers has to be
328
provided by the user. </p>
329
<p>Defaults to <c>false</c>. </p>
334
<marker id="set_options"></marker>
339
<name>set_options(Options) -> </name>
340
<name>set_options(Options, Profile) -> ok | {error, Reason}</name>
341
<fsummary>Sets options to be used for subsequent requests.</fsummary>
343
<v>Options = [Option]</v>
344
<v>Option = {proxy, {Proxy, NoProxy}} | {max_sessions, MaxSessions} |
345
{max_keep_alive_length, MaxKeepAlive} | {keep_alive_timeout, KeepAliveTimeout} |
346
{max_pipeline_length, MaxPipeline} | {pipeline_timeout, PipelineTimeout} |
347
{cookies | CookieMode} |
348
{ipfamily, IpFamily} | {ip, IpAddress} | {port, Port} |
349
{verbose, VerboseMode} </v>
350
<v>Proxy = {Hostname, Port}</v>
351
<v>Hostname = string() </v>
352
<d>ex: "localhost" or "foo.bar.se"</d>
353
<v>Port = integer()</v>
355
<v>NoProxy = [NoProxyDesc]</v>
356
<v>NoProxyDesc = DomainDesc | HostName | IPDesc</v>
357
<v>DomainDesc = "*.Domain"</v>
358
<d>ex: "*.ericsson.se"</d>
359
<v>IpDesc = string()</v>
360
<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>
361
<v>MaxSessions = integer() </v>
362
<d>Default is <em>2</em>.
363
Maximum number of persistent connections to a host.</d>
364
<v>MaxKeepAlive = integer() </v>
365
<d>Default is <em>5</em>.
366
Maximum number of outstanding requests on the same connection to
368
<v>KeepAliveTimeout = integer() </v>
369
<d>Default is <em>120000</em> (= 2 min).
370
If a persistent connection is idle longer than the
371
keep_alive_timeout the client will close the connection.
372
The server may also have a such a time out but you should
374
<v>MaxPipeline = integer() </v>
375
<d>Default is <em>2</em>.
376
Maximum number of outstanding requests on a pipelined connection to a host.</d>
377
<v>PipelineTimeout = integer() </v>
378
<d>Default is <em>0</em>,
379
which will result in pipelining not being used.
380
If a persistent connection is idle longer than the
381
pipeline_timeout the client will close the connection. </d>
382
<v>CookieMode = enabled | disabled | verify </v>
383
<d>Default is <em>disabled</em>.
384
If Cookies are enabled all valid cookies will automatically be
385
saved in the client manager's cookie database.
386
If the option verify is used the function http:verify_cookie/2
387
has to be called for the cookie to be saved.</d>
388
<v>IpFamily = inet | inet6 | inet6fb4 </v>
389
<d>By default <em>inet</em>.
390
When it is set to <c>inet6fb4</c> you can use both ipv4 and ipv6.
391
It first tries <c>inet6</c> and if that does not works falls back to <c>inet</c>.
392
The option is here to provide a workaround for buggy ipv6 stacks to ensure that
393
ipv4 will always work.</d>
394
<v>IpAddress = ip_address() </v>
395
<d>If the host has several network interfaces, this option specifies which one to use.
396
See gen_tcp:connect/3,4 for more info. </d>
397
<v>Port = integer() </v>
398
<d>Specify which local port number to use.
399
See gen_tcp:connect/3,4 for more info. </d>
400
<v>VerboseMode = false | verbose | debug | trace </v>
401
<d>Default is <em>false</em>.
402
This option is used to switch on (or off)
403
different levels of erlang trace on the client.
404
It is a debug feature.</d>
405
<v>Profile = profile()</v>
408
<p>Sets options to be used for subsequent
411
<p>If possible the client will keep its connections
412
alive and use persistent connections
413
with or without pipeline depending on configuration
414
and current circumstances. The HTTP/1.1 specification does not
415
provide a guideline for how many requests that would be
416
ideal to be sent on a persistent connection,
417
this very much depends on the
418
application. Note that a very long queue of requests may cause a
419
user perceived delays as earlier request may take a long time
420
to complete. The HTTP/1.1 specification does suggest a
421
limit of 2 persistent connections per server, which is the
422
default value of the max_sessions option. </p>
425
<marker id="stream_next"></marker>
430
<name>stream_next(Pid) -> ok</name>
431
<fsummary> Triggers the next message to be streamed, e.i.
432
same behavior as active once for sockets.
435
<v>Pid = pid() - as received in the stream_start message</v>
438
<p>Triggers the next message to be streamed, e.i.
439
same behavior as active once for sockets.</p>
441
<marker id="verify_cookie"></marker>
446
<name>verify_cookie(SetCookieHeaders, Url) -> </name>
447
<name>verify_cookie(SetCookieHeaders, Url, Profile) -> ok | {error, Reason}</name>
448
<fsummary>Saves the cookies defined in SetCookieHeaders in the client profile's cookie database.</fsummary>
450
<v>SetCookieHeaders = headers() - where field = "set-cookie"</v>
452
<v>Profile = profile()</v>
455
<p>Saves the cookies defined in SetCookieHeaders
456
in the client profile's cookie database. You need to
457
call this function if you set the option cookies to verify.
458
If no profile is specified the default profile will be used.
461
<marker id="cookie_header"></marker>
466
<name>cookie_header(Url) -> </name>
467
<name>cookie_header(Url, Profile) -> header() | {error, Rason}</name>
468
<fsummary>Returns the cookie header that would be sent when
469
making a request to Url using the profile Profile.</fsummary>
472
<v>Profile = profile()</v>
475
<p>Returns the cookie header that would be sent
476
when making a request to Url using the profile Profile.
477
If no profile is specified the default profile will be used.
484
<title>SEE ALSO</title>
485
<p>RFC 2616, <seealso marker="inets">inets(3)</seealso>,
486
<seealso marker="ssl:ssl">ssl(3)</seealso>