httpc
MODULE
MODULE SUMMARY
DESCRIPTION
This module provides the API to a HTTP/1.1 compatible client according to RFC 2616, caching is currently not supported.
When starting the Inets application a manager process for the default profile will be started. The functions in this API that do not explicitly use a profile will access the default profile. A profile keeps track of proxy options, cookies and other options that can be applied to more than one request.
If the scheme https is used the ssl application needs to be started. When https links needs to go through a proxy the CONNECT method extension to HTTP-1.1 is used to establish a tunnel and then the connection is upgraded to TLS, however "TLS upgrade" according to RFC 2817 is not supported.
Also note that pipelining will only be used if the pipeline timeout is set, otherwise persistent connections without pipelining will be used e.i. the client always waits for the previous response before sending the next request.
There are some usage examples in the Inets User's Guide.
COMMON DATA TYPES
Type definitions that are used more than once in this module:
boolean() = true | false string() = list of ASCII characters request_id() = ref() profile() = atom() path() = string() representing a file path or directory path ip_address() = See inet(3) socket_opt() = See the Options used by gen_tcp(3) and ssl(3) connect(s)
HTTP DATA TYPES
Type definitions that are related to HTTP:
For more information about HTTP see rfc 2616
method() = head | get | put | post | trace | options | delete request() = {url(), headers()} | {url(), headers(), content_type(), body()} url() = string() - Syntax according to the URI definition in rfc 2396, ex: "http://www.erlang.org" status_line() = {http_version(), status_code(), reason_phrase()} http_version() = string() ex: "HTTP/1.1" status_code() = integer() reason_phrase() = string() content_type() = string() headers() = [header()] header() = {field(), value()} field() = string() value() = string() body() = string() | binary() | {fun(accumulator()) -> body_processing_result(), accumulator()} | {chunkify, fun(accumulator()) -> body_processing_result(), accumulator()} body_processing_result() = eof | {ok, iolist(), accumulator()} accumulator() = term() filename() = string()
SSL DATA TYPES
See ssl(3) for information about ssl options (ssloptions()).
HTTP CLIENT SERVICE START/STOP
A HTTP client can be configured to start when starting the inets application or started dynamically in runtime by calling the inets application API inets:start(httpc, ServiceConfig), or inets:start(httpc, ServiceConfig, How) see inets(3). Below follows a description of the available configuration options.
- {profile, profile()}
- Name of the profile, see common data types below, this option is mandatory.
- {data_dir, path()}
- Directory where the profile may save persistent data, if omitted all cookies will be treated as session cookies.
The client can be stopped using inets:stop(httpc, Pid) or inets:stop(httpc, Profile).
EXPORTS
request(Url) ->
request(Url, Profile) -> {ok, Result} | {error, Reason}
Types:
request(Method, Request, HTTPOptions, Options) ->
request(Method, Request, HTTPOptions, Options, Profile) -> {ok, Result} | {ok, saved_to_file} | {error, Reason}
Types:
Sends a HTTP-request. The function can be both synchronous and asynchronous. In the later case the function will return {ok, RequestId} and later on the information will be delivered to the receiver depending on that value.
Http option (http_option()) details:
- timeout
-
Timeout time for the request.
The clock starts ticking as soon as the request has been sent.
Time is in milliseconds.
Defaults to infinity.
- connect_timeout
-
Connection timeout time, used during the initial request, when the client is connecting to the server.
Time is in milliseconds.
Defaults to the value of the timeout option.
- ssl
-
This is the default ssl config option, currently defaults to essl, see below.
Defaults to [].
- essl
-
If using the Erlang based implementation of SSL, these SSL-specific options are used.
Defaults to [].
- autoredirect
-
Should the client automatically retrieve the information from the new URI and return that as the result instead of a 30X-result code.
Note that for some 30X-result codes automatic redirect is not allowed. In these cases the 30X-result will always be returned.
Defaults to true.
- proxy_auth
-
A proxy-authorization header using the provided user name and password will be added to the request.
- version
-
Can be used to make the client act as an HTTP/1.0 or HTTP/0.9 client. By default this is an HTTP/1.1 client. When using HTTP/1.0 persistent connections will not be used.
Defaults to the string "HTTP/1.1".
- relaxed
-
If set to true workarounds for known server deviations from the HTTP-standard are enabled.
Defaults to false.
- url_encode
-
Will apply Percent-encoding, also known as URL encoding on the URL.
Defaults to false.
Option (option()) details:
- sync
-
Shall the request be synchronous or asynchronous.
Defaults to true.
- stream
-
Streams the body of a 200 or 206 response to the calling process or to a file. When streaming to the calling process using the option self the following stream messages will be sent to that process: {http, {RequestId, stream_start, Headers}}, {http, {RequestId, stream, BinBodyPart}}, {http, {RequestId, stream_end, Headers}}. When streaming to to the calling processes using the option {self, once} the first message will have an additional element e.i. {http, {RequestId, stream_start, Headers, Pid}}, this is the process id that should be used as an argument to http:stream_next/1 to trigger the next message to be sent to the calling process.
Note that it is possible that chunked encoding will add headers so that there are more headers in the stream_end message than in the stream_start. When streaming to a file and the request is asynchronous the message {http, {RequestId, saved_to_file}} will be sent.
Defaults to none.
- body_format
-
Defines if the body shall be delivered as a string or as a binary. This option is only valid for the synchronous request.
Defaults to string.
- full_result
-
Should a "full result" be returned to the caller (that is, the body, the headers and the entire status-line) or not (the body and the status code).
Defaults to true.
- header_as_is
-
Shall the headers provided by the user be made lower case or be regarded as case sensitive.
Note that the http standard requires them to be case insenstive. This feature should only be used if there is no other way to communicate with the server or for testing purpose. Also note that when this option is used no headers will be automatically added, all necessary headers have to be provided by the user.
Defaults to false.
- socket_opts
-
Socket options to be used for this and subsequent request(s).
Overrides any value set by the set_options function.
Note that the validity of the options are not checked in any way.
Note that this may change the socket behaviour (see inet:setopts/2) for an already existing one, and therefore an already connected request handler.
By default the socket options set by the set_options/1,2 function are used when establishing a connection.
- receiver
-
Defines how the client will deliver the result of an asynchroneous request (sync has the value false).
- pid()
-
Message(s) will be sent to this process in the format:
{http, ReplyInfo}
- function/1
-
Information will be delivered to the receiver via calls to the provided fun:
Receiver(ReplyInfo)
- {Module, Function, Args}
-
Information will be delivered to the receiver via calls to the callback function:
apply(Module, Function, [ReplyInfo | Args])
In all of the above cases, ReplyInfo has the following structure:
{RequestId, saved_to_file} {RequestId, {error, Reason}} {RequestId, Result} {RequestId, stream_start, Headers} {RequestId, stream_start, Headers, HandlerPid} {RequestId, stream, BinBodyPart} {RequestId, stream_end, Headers}
Defaults to the pid() of the process calling the request function (self()).
- ipv6_host_with_brackets
-
When parsing the Host-Port part of an URI with a IPv6 address with brackets, shall we retain those brackets (true) or strip them (false).
Defaults to false.
cancel_request(RequestId) ->
cancel_request(RequestId, Profile) -> ok
Types:
Cancels an asynchronous HTTP-request. Note this does not guarantee that the request response will not be delivered, as it is asynchronous the the request may already have been completed when the cancellation arrives.
set_options(Options) ->
set_options(Options, Profile) -> ok | {error, Reason}
Types:
Sets options to be used for subsequent requests.
If possible the client will keep its connections alive and use persistent connections with or without pipeline depending on configuration and current circumstances. The HTTP/1.1 specification does not provide a guideline for how many requests would be ideal to be sent on a persistent connection, this very much depends on the application. Note that a very long queue of requests may cause a user perceived delay as earlier requests may take a long time to complete. The HTTP/1.1 specification does suggest a limit of 2 persistent connections per server, which is the default value of the max_sessions option.
get_options(OptionItems) -> {ok, Values} | {error, Reason}
get_options(OptionItems, Profile) -> {ok, Values} | {error, Reason}
Types:
Types:
store_cookies(SetCookieHeaders, Url) ->
store_cookies(SetCookieHeaders, Url, Profile) -> ok | {error, Reason}
Types:
Saves the cookies defined in SetCookieHeaders in the client profile's cookie database. You need to call this function if you have set the option cookies to verify. If no profile is specified the default profile will be used.
cookie_header(Url) ->
cookie_header(Url, Profile | Opts) -> header() | {error, Reason}
cookie_header(Url, Opts, Profile) -> header() | {error, Reason}
Types:
Returns the cookie header that would be sent when making a request to Url using the profile Profile. If no profile is specified the default profile will be used.
The option ipv6_host_with_bracket deals with how to parse IPv6 addresses. See the Options argument of the request/4,5 for more info.
reset_cookies() -> void()
reset_cookies(Profile) -> void()
Types:
Resets (clears) the cookie database for the specified Profile. If no profile is specified the default profile will be used.
which_cookies() -> cookies()
which_cookies(Profile) -> cookies()
Types:
This function produces a list of the entire cookie database. It is intended for debugging/testing purposes. If no profile is specified the default profile will be used.
which_sessions() -> session_info()
which_sessions(Profile) -> session_info()
Types:
This function produces a slightly processed dump of the session database. It is intended for debugging. If no profile is specified the default profile will be used.
info() -> list()
info(Profile) -> list()
Types:
This function produces a list of miscelleneous info. It is intended for debugging. If no profile is specified the default profile will be used.
SEE ALSO
RFC 2616, inets(3), gen_tcp(3), ssl(3)