httpc(3erl) | Erlang Module Definition | httpc(3erl) |
httpc - An HTTP/1.1 client
This module provides the API to an HTTP/1.1 compatible client according to RFC 2616. Caching is not supported.
If the scheme https is used, the SSL application must be started. When https links need 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 2817is not supported.
Pipelining is only used if the pipeline time-out is set, otherwise persistent connections without pipelining are used. That is, the client always waits for the previous response before sending the next request.
Some examples are provided in the Inets User's Guide.
Type definitions that are used more than once in this module:
boolean() = true | false
string() = list of ASCII characters
request_id() = reference()
profile() = atom()
path() = string() representing a file path or directory path
ip_address() = See the inet(3erl) manual page in Kernel.
socket_opt() = See the options used by gen_tcp(3erl) gen_tcp(3erl) and ssl(3erl) connect(s)
Type definitions related to HTTP:
method() = head | get | put | post | trace | options | delete | patch
| {url(), headers(), content_type(), body()}
url() = string() syntax according to the URI definition in RFC 3986, for example "http://www.erlang.org"
For example, in order to send an HTTP GET request with the URI http://localhost/foo%25bar, the percent character must be percent-encoded when creating the request: httpc:request("http://localhost/foo%2525bar").
status_line() = {http_version(), status_code(), reason_phrase()}
http_version() = string(), for example, "HTTP/1.1"
status_code() = integer()
reason_phrase() = string()
content_type() = string()
headers() = [header()]
header() = {field(), value()}
field() = string()
value() = string()
| {fun(accumulator())
-> body_processing_result(), accumulator()}
| {chunkify, fun(accumulator())
-> body_processing_result(), accumulator()}
body_processing_result() = eof | {ok, iolist(), accumulator()}
accumulator() = term()
filename() = string()
For more information about HTTP, see RFC 2616.
See ssl(3erl) for information about SSL options (ssloptions()).
An 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(3erl). The configuration options are as follows:
The client can be stopped using inets:stop(httpc, Pid) or inets:stop(httpc, Profile).
cancel_request(RequestId) ->
cancel_request(RequestId, Profile) -> ok
Types:
Cancels an asynchronous HTTP request. Notice that this does not guarantee that the request response is not delivered. Because it is asynchronous, the request can already have been completed when the cancellation arrives.
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 have been sent when making a request to Url using profile Profile. If no profile is specified, the default profile is used.
Option ipv6_host_with_bracket deals with how to parse IPv6 addresses. For details, see argument Options of request/[4,5].
get_options(OptionItems) -> {ok, Values} | {error,
Reason}
get_options(OptionItems, Profile) -> {ok, Values} | {error, Reason}
Types:
Retrieves the options currently used by the client.
info() -> list()
info(Profile) -> list()
Types:
Produces a list of miscellaneous information. Intended for debugging. If no profile is specified, the default profile is used.
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 is used.
request(Url) ->
request(Url, Profile) -> {ok, Result} | {error, Reason}
Types:
Equivalent to httpc:request(get, {Url, []}, [], []).
request(Method, Request, HTTPOptions, Options) ->
request(Method, Request, HTTPOptions, Options, Profile) -> {ok, Result}
| {ok, saved_to_file} | {error, Reason}
Types:
Sends an HTTP request. The function can be both synchronous and asynchronous. In the latter case, the function returns {ok, RequestId} and then the information is delivered to the receiver depending on that value.
HTTP option (http_option()) details:
The clock starts ticking when the request is sent.
Time is in milliseconds.
Default is infinity.
Time is in milliseconds.
Default is the value of option timeout.
Defaults to []. See ssl:connect/[2,3,4] for available options.
For some 30X-result codes, automatic redirect is not allowed. In these cases the 30X-result is always returned.
Default is true.
Default is the string "HTTP/1.1".
Default is false.
Option (option()) details:
Default is true.
When streaming to the calling processes using option {self, once}, the first message has an extra element, that is, {http, {RequestId, stream_start, Headers, Pid}}. This is the process id to be used as an argument to httpc:stream_next/1 to trigger the next message to be sent to the calling process.
Notice that chunked encoding can add headers so that there are more headers in the stream_end message than in stream_start. When streaming to a file and the request is asynchronous, the message {http, {RequestId, saved_to_file}} is sent.
Default is none.
Default is string.
Default is true.
The HTTP standard requires them to be case insensitive. Use this feature only if there is no other way to communicate with the server or for testing purpose. When this option is used, no headers are automatically added. All necessary headers must be provided by the user.
Default is false.
Overrides any value set by function set_options.
The validity of the options is not checked by the HTTP client they are assumed to be correct and passed on to ssl application and inet driver, which may reject them if they are not correct.
By default the socket options set by function set_options/[1,2] are used when establishing a connection.
In all of these 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}
Default is the pid of the process calling the request function (self()).
Default is false.
set_options(Options) ->
set_options(Options, Profile) -> ok | {error, Reason}
Types:
Sets options to be used for subsequent requests.
A long queue of requests can cause a user-perceived delay, as earlier requests can take a long time to complete. The HTTP/1.1 specification suggests a limit of two persistent connections per server, which is the default value of option max_sessions.
The current implementation assumes the requests to the same host, port combination will use the same socket options.
store_cookies(SetCookieHeaders, Url) ->
store_cookies(SetCookieHeaders, Url, Profile) -> ok | {error,
Reason}
Types:
Saves the cookies defined in SetCookieHeaders in the client profile cookie database. Call this function if option cookies is set to verify. If no profile is specified, the default profile is used.
stream_next(Pid) -> ok
Types:
Triggers the next message to be streamed, that is, the same behavior as active ones for sockets.
which_cookies() -> cookies()
which_cookies(Profile) -> cookies()
Types:
Produces a list of the entire cookie database. Intended for debugging/testing purposes. If no profile is specified, the default profile is used.
which_sessions() -> session_info()
which_sessions(Profile) -> session_info()
Types:
This function is intended for debugging only. It produces a slightly processed dump of the session database. The first list of the session information tuple will contain session information on an internal format. The last two lists of the session information tuple should always be empty if the code is working as intended. If no profile is specified, the default profile is used.
RFC 2616, inets(3erl), gen_tcp(3erl), ssl(3erl)
inets 7.3.2 | Ericsson AB |