.TH httpc 3erl "inets 7.3.2" "Ericsson AB" "Erlang Module Definition" .SH NAME httpc \- An HTTP/1.1 client .SH DESCRIPTION .LP This module provides the API to an HTTP/1\&.1 compatible client according to RFC 2616\&. Caching is not supported\&. .LP .RS -4 .B Note: .RE When starting the \fIInets\fR\& application, a manager process for the default profile is started\&. The functions in this API that do not explicitly use a profile accesses the default profile\&. A profile keeps track of proxy options, cookies, and other options that can be applied to more than one request\&. .LP If the scheme \fIhttps\fR\& is used, the \fISSL\fR\& application must be started\&. When \fIhttps\fR\& 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\&. .LP 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\&. .LP Some examples are provided in the Inets User\&'s Guide\&. .SH "DATA TYPES" .LP Type definitions that are used more than once in this module: .LP \fIboolean() = true | false\fR\& .LP \fIstring()\fR\& = list of ASCII characters .LP \fIrequest_id() = reference()\fR\& .LP \fIprofile() = atom()\fR\& .LP \fIpath() = string()\fR\& representing a file path or directory path .LP \fIip_address()\fR\& = See the inet(3erl) manual page in Kernel\&. .LP \fIsocket_opt()\fR\& = See the options used by gen_tcp(3erl) \fIgen_tcp(3erl)\fR\& and ssl(3erl) connect(s) .SH "HTTP DATA TYPES" .LP Type definitions related to HTTP: .LP \fImethod() = head | get | put | post | trace | options | delete | patch\fR\& .RS 2 .TP 2 .B \fIrequest()\fR\&: = \fI{url(), headers()}\fR\& .RS 2 .LP | \fI{url(), headers(), content_type(), body()}\fR\& .RE .RE .LP \fIurl() = string()\fR\& syntax according to the URI definition in RFC 3986, for example \fI"http://www\&.erlang\&.org"\fR\& .LP .RS -4 .B Warning: .RE Please note that httpc normalizes input URIs before internal processing and special care shall be taken when the URI has percent ("%") characters\&. A percent serves as the indicator for percent-encoded octets and it must be percent-encoded as "%25" for that octet to be used as data within the URI\&. .LP For example, in order to send an HTTP GET request with the URI \fIhttp://localhost/foo%25bar\fR\&, the percent character must be percent-encoded when creating the request: \fIhttpc:request("http://localhost/foo%2525bar")\&.\fR\& .LP \fIstatus_line() = {http_version(), status_code(), reason_phrase()}\fR\& .LP \fIhttp_version() = string()\fR\&, for example, \fI"HTTP/1\&.1"\fR\& .LP \fIstatus_code() = integer()\fR\& .LP \fIreason_phrase() = string()\fR\& .LP \fIcontent_type() = string()\fR\& .LP \fIheaders() = [header()]\fR\& .LP \fIheader() = {field(), value()}\fR\& .LP \fIfield() = string()\fR\& .LP \fIvalue() = string()\fR\& .RS 2 .TP 2 .B \fIbody()\fR\&: = \fIstring() | binary()\fR\& .RS 2 .LP | \fI{fun(accumulator())\fR\& .RE .RS 2 .LP \fI -> body_processing_result(), accumulator()}\fR\& .RE .RS 2 .LP | \fI{chunkify, fun(accumulator())\fR\& .RE .RS 2 .LP \fI -> body_processing_result(), accumulator()}\fR\& .RE .RE .LP \fIbody_processing_result() = eof | {ok, iolist(), accumulator()}\fR\& .LP \fIaccumulator() = term()\fR\& .LP \fIfilename() = string()\fR\& .LP For more information about HTTP, see RFC 2616\&. .SH "SSL DATA TYPES" .LP See ssl(3erl) for information about \fISSL\fR\& options (\fIssloptions()\fR\&)\&. .SH "HTTP CLIENT SERVICE START/STOP" .LP An HTTP client can be configured to start when starting the \fIInets\fR\& application or started dynamically in runtime by calling the \fIInets\fR\& application API \fIinets:start(httpc, ServiceConfig)\fR\& or \fIinets:start(httpc, ServiceConfig, How)\fR\&, see inets(3erl)\&. The configuration options are as follows: .RS 2 .TP 2 .B {profile, profile()}: Name of the profile, see DATA TYPES\&. This option is mandatory\&. .TP 2 .B {data_dir, path()}: Directory where the profile can save persistent data\&. If omitted, all cookies are treated as session cookies\&. .RE .LP The client can be stopped using \fIinets:stop(httpc, Pid)\fR\& or \fIinets:stop(httpc, Profile)\fR\&\&. .SH EXPORTS .LP .B cancel_request(RequestId) -> .br .B cancel_request(RequestId, Profile) -> ok .br .RS .LP Types: .RS 3 RequestId = request_id() - A unique identifier as returned by request/4 .br Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE .RE .RE .RS .LP 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\&. .RE .LP .B cookie_header(Url) -> .br .B cookie_header(Url, Profile | Opts) -> header() | {error, Reason} .br .B cookie_header(Url, Opts, Profile) -> header() | {error, Reason} .br .RS .LP Types: .RS 3 Url = url() .br Opts = [cookie_header_opt()] .br Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\&\&. .RE cookie_header_opt() = {ipv6_host_with_brackets, boolean()} .br .RE .RE .RS .LP Returns the cookie header that would have been sent when making a request to \fIUrl\fR\& using profile \fIProfile\fR\&\&. If no profile is specified, the default profile is used\&. .LP Option \fIipv6_host_with_bracket\fR\& deals with how to parse IPv6 addresses\&. For details, see argument \fIOptions\fR\& of request/[4,5]\&. .RE .LP .B get_options(OptionItems) -> {ok, Values} | {error, Reason} .br .B get_options(OptionItems, Profile) -> {ok, Values} | {error, Reason} .br .RS .LP Types: .RS 3 OptionItems = all | [option_item()] .br option_item() = proxy | https_proxy | max_sessions | keep_alive_timeout | max_keep_alive_length | pipeline_timeout | max_pipeline_length | cookies | ipfamily | ip | port | socket_opts | verbose | unix_socket .br Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can used\&. .RE Values = [{option_item(), term()}] .br Reason = term() .br .RE .RE .RS .LP Retrieves the options currently used by the client\&. .RE .LP .B info() -> list() .br .B info(Profile) -> list() .br .RS .LP Types: .RS 3 Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE .RE .RE .RS .LP Produces a list of miscellaneous information\&. Intended for debugging\&. If no profile is specified, the default profile is used\&. .RE .LP .B reset_cookies() -> void() .br .B reset_cookies(Profile) -> void() .br .RS .LP Types: .RS 3 Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE .RE .RE .RS .LP Resets (clears) the cookie database for the specified \fIProfile\fR\&\&. If no profile is specified the default profile is used\&. .RE .LP .B request(Url) -> .br .B request(Url, Profile) -> {ok, Result} | {error, Reason} .br .RS .LP Types: .RS 3 Url = url() .br Result = {status_line(), headers(), Body} | {status_code(), Body} | request_id() .br Body = string() | binary() .br Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE Reason = term() .br .RE .RE .RS .LP Equivalent to \fIhttpc:request(get, {Url, []}, [], [])\fR\&\&. .RE .LP .B request(Method, Request, HTTPOptions, Options) -> .br .B request(Method, Request, HTTPOptions, Options, Profile) -> {ok, Result} | {ok, saved_to_file} | {error, Reason} .br .RS .LP Types: .RS 3 Method = method() .br Request = request() .br HTTPOptions = http_options() .br http_options() = [http_option()] .br http_option() = {timeout, timeout()} | {connect_timeout, timeout()} | {ssl, ssloptions()} | {essl, ssloptions()} | {autoredirect, boolean()} | {proxy_auth, {userstring(), passwordstring()}} | {version, http_version()} | {relaxed, boolean()} .br timeout() = integer() >= 0 | infinity .br Options = options() .br options() = [option()] .br option() = {sync, boolean()} | {stream, stream_to()} | {body_format, body_format()} | {full_result, boolean()} | {headers_as_is, boolean() | {socket_opts, socket_opts()} | {receiver, receiver()} | {ipv6_host_with_brackets, boolean()} .br stream_to() = none | self | {self, once} | filename() .br socket_opts() = [socket_opt()] .br receiver() = pid() | function()/1 | {Module, Function, Args} .br Module = atom() .br Function = atom() .br Args = list() .br body_format() = string | binary .br Result = {status_line(), headers(), Body} | {status_code(), Body} | request_id() .br Body = string() | binary() .br Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE Reason = term() .br .RE .RE .RS .LP Sends an HTTP request\&. The function can be both synchronous and asynchronous\&. In the latter case, the function returns \fI{ok, RequestId}\fR\& and then the information is delivered to the \fIreceiver\fR\& depending on that value\&. .LP HTTP option (\fIhttp_option()\fR\&) details: .RS 2 .TP 2 .B \fItimeout\fR\&: Time-out time for the request\&. .RS 2 .LP The clock starts ticking when the request is sent\&. .RE .RS 2 .LP Time is in milliseconds\&. .RE .RS 2 .LP Default is \fIinfinity\fR\&\&. .RE .TP 2 .B \fIconnect_timeout\fR\&: Connection time-out time, used during the initial request, when the client is \fIconnecting\fR\& to the server\&. .RS 2 .LP Time is in milliseconds\&. .RE .RS 2 .LP Default is the value of option \fItimeout\fR\&\&. .RE .TP 2 .B \fIssl\fR\&: This is the \fISSL/TLS\fR\& connectin configuration option\&. .RS 2 .LP Defaults to \fI[]\fR\&\&. See ssl:connect/[2,3,4] for available options\&. .RE .TP 2 .B \fIautoredirect\fR\&: The client automatically retrieves the information from the new URI and returns that as the result, instead of a 30X-result code\&. .RS 2 .LP For some 30X-result codes, automatic redirect is not allowed\&. In these cases the 30X-result is always returned\&. .RE .RS 2 .LP Default is \fItrue\fR\&\&. .RE .TP 2 .B \fIproxy_auth\fR\&: A proxy-authorization header using the provided username and password is added to the request\&. .TP 2 .B \fIversion\fR\&: Can be used to make the client act as an \fIHTTP/1\&.0\fR\& or \fIHTTP/0\&.9\fR\& client\&. By default this is an \fIHTTP/1\&.1\fR\& client\&. When using \fIHTTP/1\&.0\fR\& persistent connections are not used\&. .RS 2 .LP Default is the string \fI"HTTP/1\&.1"\fR\&\&. .RE .TP 2 .B \fIrelaxed\fR\&: If set to \fItrue\fR\&, workarounds for known server deviations from the HTTP-standard are enabled\&. .RS 2 .LP Default is \fIfalse\fR\&\&. .RE .RE .LP Option (\fIoption()\fR\&) details: .RS 2 .TP 2 .B \fIsync\fR\&: Option for the request to be synchronous or asynchronous\&. .RS 2 .LP Default is \fItrue\fR\&\&. .RE .TP 2 .B \fIstream\fR\&: Streams the body of a 200 or 206 response to the calling process or to a file\&. When streaming to the calling process using option \fIself\fR\&, the following stream messages are sent to that process: \fI{http, {RequestId, stream_start, Headers}}, {http, {RequestId, stream, BinBodyPart}}, and {http, {RequestId, stream_end, Headers}}\fR\&\&. .RS 2 .LP When streaming to the calling processes using option \fI{self, once}\fR\&, the first message has an extra element, that is, \fI{http, {RequestId, stream_start, Headers, Pid}}\fR\&\&. This is the process id to be used as an argument to \fIhttpc:stream_next/1\fR\& to trigger the next message to be sent to the calling process\&. .RE .RS 2 .LP Notice that chunked encoding can add headers so that there are more headers in the \fIstream_end\fR\& message than in \fIstream_start\fR\&\&. When streaming to a file and the request is asynchronous, the message \fI{http, {RequestId, saved_to_file}}\fR\& is sent\&. .RE .RS 2 .LP Default is \fInone\fR\&\&. .RE .TP 2 .B \fIbody_format\fR\&: Defines if the body is to be delivered as a string or binary\&. This option is only valid for the synchronous request\&. .RS 2 .LP Default is \fIstring\fR\&\&. .RE .TP 2 .B \fIfull_result\fR\&: Defines if a "full result" is to be returned to the caller (that is, the body, the headers, and the entire status line) or not (the body and the status code)\&. .RS 2 .LP Default is \fItrue\fR\&\&. .RE .TP 2 .B \fIheaders_as_is\fR\&: Defines if the headers provided by the user are to be made lower case or to be regarded as case sensitive\&. .RS 2 .LP 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\&. .RE .RS 2 .LP Default is \fIfalse\fR\&\&. .RE .TP 2 .B \fIsocket_opts\fR\&: Socket options to be used for this request\&. .RS 2 .LP Overrides any value set by function set_options\&. .RE .RS 2 .LP The validity of the options is \fInot\fR\& 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\&. .RE .LP .RS -4 .B Note: .RE Persistent connections are not supported when setting the \fIsocket_opts\fR\& option\&. When \fIsocket_opts\fR\& is not set the current implementation assumes the requests to the same host, port combination will use the same socket options\&. .RS 2 .LP By default the socket options set by function set_options/[1,2] are used when establishing a connection\&. .RE .TP 2 .B \fIreceiver\fR\&: Defines how the client delivers the result of an asynchronous request (\fIsync\fR\& has the value \fIfalse\fR\&)\&. .RS 2 .TP 2 .B \fIpid()\fR\&: Messages are sent to this process in the format \fI{http, ReplyInfo}\fR\&\&. .TP 2 .B \fIfunction/1\fR\&: Information is delivered to the receiver through calls to the provided fun \fIReceiver(ReplyInfo)\fR\&\&. .TP 2 .B \fI{Module, Function, Args}\fR\&: Information is delivered to the receiver through calls to the callback function \fIapply(Module, Function, [ReplyInfo | Args])\fR\&\&. .RE .RS 2 .LP In all of these cases, \fIReplyInfo\fR\& has the following structure: .RE .LP .nf {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} .fi .RS 2 .LP Default is the \fIpid\fR\& of the process calling the request function (\fIself()\fR\&)\&. .RE .TP 2 .B \fIipv6_host_with_brackets\fR\&: Defines when parsing the Host-Port part of an URI with an IPv6 address with brackets, if those brackets are to be retained (\fItrue\fR\&) or stripped (\fIfalse\fR\&)\&. .RS 2 .LP Default is \fIfalse\fR\&\&. .RE .RE .RE .LP .B set_options(Options) -> .br .B set_options(Options, Profile) -> ok | {error, Reason} .br .RS .LP Types: .RS 3 Options = [Option] .br Option = {proxy, {Proxy, NoProxy}} .br | {https_proxy, {Proxy, NoProxy}} .br | {max_sessions, MaxSessions} .br | {max_keep_alive_length, MaxKeepAlive} .br | {keep_alive_timeout, KeepAliveTimeout} .br | {max_pipeline_length, MaxPipeline} .br | {pipeline_timeout, PipelineTimeout} .br | {cookies, CookieMode} .br | {ipfamily, IpFamily} .br | {ip, IpAddress} .br | {port, Port} .br | {socket_opts, socket_opts()} .br | {verbose, VerboseMode} .br | {unix_socket, UnixSocket} .br Proxy = {Hostname, Port} .br Hostname = string() .br .RS 2 Example: "localhost" or "foo\&.bar\&.se" .RE Port = integer() .br .RS 2 Example: 8080 .RE NoProxy = [NoProxyDesc] .br NoProxyDesc = DomainDesc | HostName | IPDesc .br DomainDesc = "*\&.Domain" .br .RS 2 Example: "*\&.ericsson\&.se" .RE IpDesc = string() .br .RS 2 Example: "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)\&. \fIproxy\fR\& defaults to \fI{undefined, []}\fR\&, that is, no proxy is configured and \fIhttps_proxy\fR\& defaults to the value of \fIproxy\fR\&\&. .RE MaxSessions = integer() .br .RS 2 Maximum number of persistent connections to a host\&. Default is \fI2\fR\&\&. .RE MaxKeepAlive = integer() .br .RS 2 Maximum number of outstanding requests on the same connection to a host\&. Default is \fI5\fR\&\&. .RE KeepAliveTimeout = integer() .br .RS 2 If a persistent connection is idle longer than the \fIkeep_alive_timeout\fR\& in milliseconds, the client closes the connection\&. The server can also have such a time-out but do not take that for granted\&. Default is \fI120000\fR\& (= 2 min)\&. .RE MaxPipeline = integer() .br .RS 2 Maximum number of outstanding requests on a pipelined connection to a host\&. Default is \fI2\fR\&\&. .RE PipelineTimeout = integer() .br .RS 2 If a persistent connection is idle longer than the \fIpipeline_timeout\fR\& in milliseconds, the client closes the connection\&. Default is \fI0\fR\&, which results in pipelining not being used\&. .RE CookieMode = enabled | disabled | verify .br .RS 2 If cookies are enabled, all valid cookies are automatically saved in the cookie database of the client manager\&. If option \fIverify\fR\& is used, function \fIstore_cookies/2\fR\& has to be called for the cookies to be saved\&. Default is \fIdisabled\fR\&\&. .RE IpFamily = inet | inet6 | local .br .RS 2 Default is \fIinet\fR\&\&. .RE IpAddress = ip_address() .br .RS 2 If the host has several network interfaces, this option specifies which one to use\&. See gen_tcp:connect/3,4 for details\&. .RE Port = integer() .br .RS 2 Local port number to use\&. See gen_tcp:connect/3,4 for details\&. .RE socket_opts() = [socket_opt()] .br .RS 2 The options are appended to the socket options used by the client\&. These are the default values when a new request handler is started (for the initial connect)\&. They are passed directly to the underlying transport (\fIgen_tcp\fR\& or \fISSL\fR\&) \fIwithout\fR\& verification\&. .RE VerboseMode = false | verbose | debug | trace .br .RS 2 Default is \fIfalse\fR\&\&. This option is used to switch on (or off) different levels of Erlang trace on the client\&. It is a debug feature\&. .RE Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE UnixSocket = path() .br .RS 2 Experimental option for sending HTTP requests over a unix domain socket\&. The value of \fIunix_socket\fR\& shall be the full path to a unix domain socket file with read/write permissions for the erlang process\&. Default is \fIundefined\fR\&\&. .RE .RE .RE .RS .LP Sets options to be used for subsequent requests\&. .LP .RS -4 .B Note: .RE If possible, the client keeps its connections alive and uses 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 that are ideal to be sent on a persistent connection\&. This depends much on the application\&. .LP 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 \fImax_sessions\fR\&\&. .LP The current implementation assumes the requests to the same host, port combination will use the same socket options\&. .RE .LP .B store_cookies(SetCookieHeaders, Url) -> .br .B store_cookies(SetCookieHeaders, Url, Profile) -> ok | {error, Reason} .br .RS .LP Types: .RS 3 SetCookieHeaders = headers() - where field = "set-cookie" .br Url = url() .br Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE .RE .RE .RS .LP Saves the cookies defined in \fISetCookieHeaders\fR\& in the client profile cookie database\&. Call this function if option \fIcookies\fR\& is set to \fIverify\fR\&\&. If no profile is specified, the default profile is used\&. .RE .LP .B stream_next(Pid) -> ok .br .RS .LP Types: .RS 3 Pid = pid() .br .RS 2 As received in the \fIstream_start message\fR\& .RE .RE .RE .RS .LP Triggers the next message to be streamed, that is, the same behavior as active ones for sockets\&. .RE .LP .B which_cookies() -> cookies() .br .B which_cookies(Profile) -> cookies() .br .RS .LP Types: .RS 3 Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE cookies() = [cookie_stores()] .br cookie_stores() = {cookies, cookies()} | {session_cookies, cookies()} .br cookies() = [cookie()] .br cookie() = term() .br .RE .RE .RS .LP Produces a list of the entire cookie database\&. Intended for debugging/testing purposes\&. If no profile is specified, the default profile is used\&. .RE .LP .B which_sessions() -> session_info() .br .B which_sessions(Profile) -> session_info() .br .RS .LP Types: .RS 3 Profile = profile() | pid() .br .RS 2 When started \fIstand_alone\fR\& only the pid can be used\&. .RE session_info() = {[session()], [term()], [term()]} .br session() = term() - Internal representation of a session .br .RE .RE .RS .LP 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\&. .RE .SH "SEE ALSO" .LP RFC 2616, inets(3erl), gen_tcp(3erl), ssl(3erl)