User's Guide
Reference Manual
Release Notes
PDF
Top

inets
User's Guide
Version 5.5.2

Expand All
Contract All

Chapters

- Introduction
  - Top of chapter
  - Purpose
  - Prerequisites
  - The Service Concept
- FTP Client
  - Top of chapter
  - Introduction
  - Using the FTP Client API
- HTTP Client
  - Top of chapter
  - Introduction
  - Configuration
  - Using the HTTP Client API
- HTTP server
  - Top of chapter
  - Introduction
  - Configuration
  - Using the HTTP Server API
  - Htaccess - User Configurable Authentication.
  - Dynamic Web Pages
  - Logging
  - Server Side Includes
  - The Erlang Web Server API
  - Inets Web Server Modules

# 3 HTTP Client

### 3.1 Introduction

The HTTP client default profile will be started when the inets application is started and is then available to all processes on that erlang node. Other profiles may also be started at application startup, or profiles can be started and stopped dynamically in runtime. Each client profile will spawn a new process to handle each request unless there is a possibility to use a persistent connection with or without pipelining. The client will add a host header and an empty te header if there are no such headers present in the request.

The client supports ipv6 as long as the underlying mechanisms also do so.

### 3.2 Configuration

What to put in the erlang node application configuration file in order to start a profile at application startup.

          [{inets, [{services, [{httpc, PropertyList}]}]}]

For valid properties see httpc(3).

### 3.3 Using the HTTP Client API

     1 > inets:start().
          ok

The following calls uses the default client profile. Use the proxy "www-proxy.mycompany.com:8000", but not for requests to localhost. This will apply to all subsequent requests

          2 > httpc:set_options([{proxy, {{"www-proxy.mycompany.com", 8000},
          ["localhost"]}}]).
          ok

An ordinary synchronous request.

          3 > {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} =
          httpc:request(get, {"http://www.erlang.org", []}, [], []).

With all default values, as above, a get request can also be written like this.

          4 > {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} =
          httpc:request("http://www.erlang.org").

An ordinary asynchronous request. The result will be sent to the calling process in the form {http, {ReqestId, Result}}

          5 > {ok, RequestId} =
          httpc:request(get, {"http://www.erlang.org", []}, [], [{sync, false}]).

In this case the calling process is the shell, so we receive the result.

          6 > receive {http, {RequestId, Result}} -> ok after 500 -> error end.
          ok

Send a request with a specified connection header.

          7 > {ok, {{NewVersion, 200, NewReasonPhrase}, NewHeaders, NewBody}} =
          httpc:request(get, {"http://www.erlang.org", [{"connection", "close"}]},
          [], []).

Start a HTTP client profile.

          8 > {ok, Pid} = inets:start(httpc, [{profile, foo}]).
          {ok, <0.45.0>}

The new profile has no proxy settings so the connection will be refused

          9 > httpc:request("http://www.erlang.org", foo).
          {error, econnrefused}

Stop a HTTP client profile.

          10 > inets:stop(httpc, foo).
          ok

Alternatively:

          10 > inets:stop(httpc, Pid).
          ok