aboutsummaryrefslogtreecommitdiffstats
path: root/lib/inets/doc/src/http_client.xml
blob: 4542211d718ede2086f0508538eef285c75892a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE chapter SYSTEM "chapter.dtd">

<chapter>
  <header>
    <copyright>
      <year>2004</year><year>2010</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.
    
      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.
    
    </legalnotice>

    <title>HTTP Client</title>
    <prepared>Ingela Anderton Andin</prepared>
    <responsible></responsible>
    <docno></docno>
    <approved></approved>
    <checked></checked>
    <date></date>
    <rev></rev>
    <file>http_client.xml</file>
  </header>

  <section>
    <title>Introduction</title>

    <p>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 <c>host</c> header and an empty
    <c>te</c> header if there are no such headers present in the request.</p>

    <p>The client supports ipv6 as long as the underlying mechanisms also do
      so.</p>
  </section>

  <section>
    <title>Configuration</title>
    <p> What to put in the erlang node application configuration file
      in order to start a profile at application startup.</p>
    <pre>
      [{inets, [{services, [{httpc, PropertyList}]}]}]
    </pre>
    <p>For valid properties see 
    <seealso marker="httpc">httpc(3)</seealso>. </p>
  </section>

  <section>
    <title>Using the HTTP Client API</title>
    <code type="erl">
 1 > inets:start().
      ok
    </code>
    <p> 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</p>
    <code type="erl">
      2 > httpc:set_options([{proxy, {{"www-proxy.mycompany.com", 8000},
      ["localhost"]}}]).
      ok
    </code>
    <p>An ordinary synchronous request. </p>
    <code type="erl">
      3 > {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} =
      httpc:request(get, {"http://www.erlang.org", []}, [], []).
    </code>
    <p>With all default values, as above, a get request can also be written
      like this.</p>
    <code type="erl">
      4 > {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} =
      httpc:request("http://www.erlang.org").
    </code>
    <p>An ordinary asynchronous request. The result will be sent
      to the calling process in the form <c>{http, {ReqestId, Result}}</c></p>
    <code type="erl">
      5 > {ok, RequestId} =
      httpc:request(get, {"http://www.erlang.org", []}, [], [{sync, false}]).
    </code>
    <p>In this case the calling process is the shell, so we receive the
      result.</p>
    <code type="erl">
      6 > receive {http, {RequestId, Result}} -> ok after 500 -> error end.
      ok
    </code>
    <p>Send a request with a specified connection header. </p>
    <code type="erl">
      7 > {ok, {{NewVersion, 200, NewReasonPhrase}, NewHeaders, NewBody}} =
      httpc:request(get, {"http://www.erlang.org", [{"connection", "close"}]},
      [], []).
    </code>

    <p>Start a HTTP client profile. </p>
   
    <code><![CDATA[
      8 > {ok, Pid} = inets:start(httpc, [{profile, foo}]).
      {ok, <0.45.0>}       
      ]]></code>
    
    <p>The new profile has no proxy settings so the connection will
      be refused</p>
      <code type="erl">
      9 > httpc:request("http://www.erlang.org", foo). 
      {error, econnrefused}
    </code>

    <p>Stop a HTTP client profile. </p>
    <code type="erl">
      10 > inets:stop(httpc, foo).
      ok
    </code>
    
    <p>Alternatively:</p>
    <code type="erl">
      10 > inets:stop(httpc, Pid).
      ok
    </code>
    
  </section>
</chapter>