[Ericsson AB]

3 HTTP client

3.1 Introduction

The http client will be started when the inets application is started and is then available to all processes on that erlang node. The client will spawn a new process to handle each request unless there is a possibility to pipeline a request. 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 underlaying mechanisms also do so.

3.2 Using the HTTP client API

 1 > application:start(inets).
      ok
    

Use the proxy "www-proxy.mycompany.com:8000", but not for requsts to localhost. This will apply to all subsequent requests

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

An ordinary synchronous request.

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

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

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

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

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

Send a request with a specified connection header.

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

Copyright © 1991-2004 Ericsson AB