summaryrefslogtreecommitdiffstats
path: root/docs/en/gun/1.1/guide/http/index.html
blob: 59cfb9a13c458331cf41e7917459c13bdd270eff (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Loïc Hoguin based on a design from (Soft10) Pol Cámara">

    <title>Nine Nines: HTTP</title>

    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700,400italic' rel='stylesheet' type='text/css'>
    <link href="/css/99s.css?r=6" rel="stylesheet">

    <link rel="shortcut icon" href="/img/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="/img/ico/apple-touch-icon-114.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="/img/ico/apple-touch-icon-72.png">
    <link rel="apple-touch-icon-precomposed" href="/img/ico/apple-touch-icon-57.png">

    
</head>


<body class="">
  <header id="page-head">
    <div id="topbar" class="container">
        <div class="row">
          <div class="span2">
            <h1 id="logo"><a href="/" title="99s">99s</a></h1>
          </div>
          <div class="span10">
            
            <div id="side-header">
              <nav>
                <ul>
                  <li><a title="Hear my thoughts" href="/articles">Articles</a></li>
  				  <li><a title="Watch my talks" href="/talks">Talks</a></li>
  				  <li class="active"><a title="Read the docs" href="/docs">Documentation</a></li>
  				  <li><a title="Request my services" href="/services">Consulting & Training</a></li>
                </ul>
              </nav> 
              <ul id="social">
                <li>
                  <a href="https://github.com/ninenines" title="Check my Github repositories"><img src="/img/ico_github.png" data-hover="/img/ico_github_alt.png" alt="Github"></a>
                </li>
                    <li>
						<a title="Contact me" href="mailto:[email protected]"><img src="/img/ico_mail.png" data-hover="/img/ico_mail_alt.png"></a>
					</li>
              </ul>
            </div>
          </div>
        </div>
    </div>


</header>

<div id="contents" class="two_col">
<div class="container">
<div class="row">
<div id="docs" class="span9 maincol">

<h1 class="lined-header"><span>HTTP</span></h1>

<p>This chapter describes how to use the Gun client for communicating with an HTTP/1.1 or HTTP/2 server.</p>
<h2 id="_streams">Streams</h2>
<p>Every time a request is initiated,  Gun creates a <em>stream</em>. A <em>stream reference</em> uniquely identifies a set of request and response and must be used to perform additional operations with a stream or to identify its messages.</p>
<p>Stream references use the Erlang <em>reference</em> data type and are therefore unique.</p>
<p>Streams can be canceled at any time. This will stop any further messages from being sent to the owner process. Depending on its capabilities, the server will also be instructed to cancel the request.</p>
<p>Canceling a stream may result in Gun dropping the connection temporarily, to avoid uploading or downloading data that will not be used.</p>
<div class="listingblock"><div class="title">Cancelling a stream</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">gun:cancel</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<h2 id="_sending_requests">Sending requests</h2>
<p>Gun provides many convenient functions for performing common operations, like GET, POST or DELETE. It also provides a general purpose function in case you need other methods.</p>
<p>The availability of these methods on the server can vary depending on the software used but also on a per-resource basis.</p>
<p>Gun will automatically set a few headers depending on the method used. For all methods however it will set the host header if it has not been provided in the request arguments.</p>
<p>This section focuses on the act of sending a request. The handling of responses will be explained further on.</p>
<h4 id="_get_and_head">GET and HEAD</h4>
<p>Use <code>gun:get/2,3,4</code> to request a resource.</p>
<div class="listingblock"><div class="title">GET &quot;/organizations/ninenines&quot;</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:get</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<div class="listingblock"><div class="title">GET &quot;/organizations/ninenines&quot; with custom headers</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:get</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>, [
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"accept"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"application/json"</font>},
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"user-agent"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"revolver/1.0"</font>}
])<font color="#990000">.</font></tt></pre>
</div></div>
<p>Note that the list of headers has the field name as a binary. The field value is iodata, which is either a binary or an iolist.</p>
<p>Use <code>gun:head/2,3,4</code> if you don&apos;t need the response body.</p>
<div class="listingblock"><div class="title">HEAD &quot;/organizations/ninenines&quot;</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:head</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<div class="listingblock"><div class="title">HEAD &quot;/organizations/ninenines&quot; with custom headers</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:head</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>, [
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"accept"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"application/json"</font>},
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"user-agent"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"revolver/1.0"</font>}
])<font color="#990000">.</font></tt></pre>
</div></div>
<p>It is not possible to send a request body with a GET or HEAD request.</p>
<h4 id="_post__put_and_patch">POST, PUT and PATCH</h4>
<p>HTTP defines three methods to create or update a resource.</p>
<p>POST is generally used when the resource identifier (URI) isn&apos;t known in advance when creating a resource. POST can also be used to replace an existing resource, although PUT is more appropriate in that situation.</p>
<p>PUT creates or replaces a resource identified by the URI.</p>
<p>PATCH provides instructions on how to modify the resource.</p>
<p>Both POST and PUT send the entire resource representation in their request body. The PATCH method can be used when this is not desirable. The request body of a PATCH method may be a partial representation or a list of instructions on how to update the resource.</p>
<p>The <code>gun:post/4,5</code>, <code>gun:put/4,5</code> and <code>gun:patch/4,5</code> functions take a body as their fourth argument. These functions do not require any body-specific header to be set, although it is always recommended to set the content-type header. Gun will set the other headers automatically.</p>
<p>In this and the following examples in this section, <code>gun:post</code> can be replaced by <code>gun:put</code> or <code>gun:patch</code> for performing a PUT or PATCH request, respectively.</p>
<div class="listingblock"><div class="title">POST &quot;/organizations/ninenines&quot;</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">Body</font> <font color="#990000">=</font> <font color="#FF0000">"{\"msg\": \"Hello world!\"}"</font>,
<font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:post</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>, [
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"content-type"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"application/json"</font>}
], <font color="#009900">Body</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>The <code>gun:post/3</code>, <code>gun:put/3</code> and <code>gun:patch/3</code> functions do not take a body in their arguments. If a body is to be provided later on, using the <code>gun:data/4</code> function, then the request headers must indicate this. This can be done by setting the content-length or content-type request headers. If these headers are not set then Gun will assume the request has no body.</p>
<p>It is recommended to send the content-length header if you know it in advance, although this is not required. If it is not set, HTTP/1.1 will use the chunked transfer-encoding, and HTTP/2 will continue normally as it is chunked by design.</p>
<div class="listingblock"><div class="title">POST &quot;/organizations/ninenines&quot; with delayed body</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">Body</font> <font color="#990000">=</font> <font color="#FF0000">"{\"msg\": \"Hello world!\"}"</font>,
<font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:post</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>, [
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"content-length"</font><font color="#990000">&gt;&gt;</font>, <b><font color="#000000">integer_to_binary</font></b>(<b><font color="#000080">length</font></b>(<font color="#009900">Body</font>))},
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"content-type"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"application/json"</font>}
]),
<b><font color="#000000">gun:data</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#FF6600">fin</font>, <font color="#009900">Body</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>The atom <code>fin</code> indicates this is the last chunk of data to be sent. You can call the <code>gun:data/4</code> function as many times as needed until you have sent the entire body. The last call must use <code>fin</code> and all the previous calls must use <code>nofin</code>. The last chunk may be empty.</p>
<div class="listingblock"><div class="title">Streaming the request body</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sendfile</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#009900">Filepath</font>) <font color="#990000">-&gt;</font>
    {<font color="#FF6600">ok</font>, <font color="#009900">IoDevice</font>} <font color="#990000">=</font> <b><font color="#000000">file:open</font></b>(<font color="#009900">Filepath</font>, [<font color="#FF6600">read</font>, <b><font color="#000080">binary</font></b>, <font color="#FF6600">raw</font>]),
    <b><font color="#000000">do_sendfile</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#009900">IoDevice</font>)<font color="#990000">.</font>

<b><font color="#000000">do_sendfile</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#009900">IoDevice</font>) <font color="#990000">-&gt;</font>
    <b><font color="#0000FF">case</font></b> <b><font color="#000000">file:read</font></b>(<font color="#009900">IoDevice</font>, <font color="#993399">8000</font>) <b><font color="#0000FF">of</font></b>
        <font color="#FF6600">eof</font> <font color="#990000">-&gt;</font>
            <b><font color="#000000">gun:data</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#FF6600">fin</font>, <font color="#990000">&lt;&lt;&gt;&gt;</font>),
            <b><font color="#000000">file:close</font></b>(<font color="#009900">IoDevice</font>);
        {<font color="#FF6600">ok</font>, <font color="#009900">Bin</font>} <font color="#990000">-&gt;</font>
            <b><font color="#000000">gun:data</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#FF6600">nofin</font>, <font color="#009900">Bin</font>),
            <b><font color="#000000">do_sendfile</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#009900">IoDevice</font>)
    <b><font color="#0000FF">end</font></b><font color="#990000">.</font></tt></pre>
</div></div>
<h4 id="_delete">DELETE</h4>
<p>Use <code>gun:delete/2,3,4</code> to delete a resource.</p>
<div class="listingblock"><div class="title">DELETE &quot;/organizations/ninenines&quot;</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:delete</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<div class="listingblock"><div class="title">DELETE &quot;/organizations/ninenines&quot; with custom headers</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:delete</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>, [
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"user-agent"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"revolver/1.0"</font>}
])<font color="#990000">.</font></tt></pre>
</div></div>
<h4 id="_options">OPTIONS</h4>
<p>Use <code>gun:options/2,3</code> to request information about a resource.</p>
<div class="listingblock"><div class="title">OPTIONS &quot;/organizations/ninenines&quot;</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:options</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<div class="listingblock"><div class="title">OPTIONS &quot;/organizations/ninenines&quot; with custom headers</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:options</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>, [
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"user-agent"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"revolver/1.0"</font>}
])<font color="#990000">.</font></tt></pre>
</div></div>
<p>You can also use this function to request information about the server itself.</p>
<div class="listingblock"><div class="title">OPTIONS &quot;*&quot;</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:options</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"*"</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<h4 id="_requests_with_an_arbitrary_method">Requests with an arbitrary method</h4>
<p>The <code>gun:request/4,5,6</code> function can be used to send requests with a configurable method name. It is mostly useful when you need a method that Gun does not understand natively.</p>
<div class="listingblock"><div class="title">Example of a TRACE request</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">gun:request</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"TRACE"</font>, <font color="#FF0000">"/"</font>, [
    {<font color="#990000">&lt;&lt;</font><font color="#FF0000">"max-forwards"</font><font color="#990000">&gt;&gt;</font>, <font color="#FF0000">"30"</font>}
])<font color="#990000">.</font></tt></pre>
</div></div>
<h2 id="_processing_responses">Processing responses</h2>
<p>All data received from the server is sent to the owner process as a message. First a <code>gun_response</code> message is sent, followed by zero or more <code>gun_data</code> messages. If something goes wrong, a <code>gun_error</code> message is sent instead.</p>
<p>The response message will inform you whether there will be data messages following. If it contains <code>fin</code> there will be no data messages. If it contains <code>nofin</code> then one or more data messages will follow.</p>
<p>When using HTTP/2 this value is sent with the frame and simply passed on in the message. When using HTTP/1.1 however Gun must guess whether data will follow by looking at the response headers.</p>
<p>You can receive messages directly, or you can use the <em>await</em> functions to let Gun receive them for you.</p>
<div class="listingblock"><div class="title">Receiving a response using receive</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">print_body</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">MRef</font>) <font color="#990000">-&gt;</font>
    <font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:get</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/"</font>),
    <b><font color="#0000FF">receive</font></b>
        {<font color="#FF6600">gun_response</font>, <font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#FF6600">fin</font>, <font color="#009900">Status</font>, <font color="#009900">Headers</font>} <font color="#990000">-&gt;</font>
            <font color="#FF6600">no_data</font>;
        {<font color="#FF6600">gun_response</font>, <font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#FF6600">nofin</font>, <font color="#009900">Status</font>, <font color="#009900">Headers</font>} <font color="#990000">-&gt;</font>
            <b><font color="#000000">receive_data</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">MRef</font>, <font color="#009900">StreamRef</font>);
        {<font color="#FF6600">'DOWN'</font>, <font color="#009900">MRef</font>, <b><font color="#000080">process</font></b>, <font color="#009900">ConnPid</font>, <font color="#009900">Reason</font>} <font color="#990000">-&gt;</font>
            <b><font color="#000000">error_logger:error_msg</font></b>(<font color="#FF0000">"Oops!"</font>),
            <b><font color="#000080">exit</font></b>(<font color="#009900">Reason</font>)
    <b><font color="#0000FF">after</font></b> <font color="#993399">1000</font> <font color="#990000">-&gt;</font>
        <b><font color="#000080">exit</font></b>(<font color="#FF6600">timeout</font>)
    <b><font color="#0000FF">end</font></b><font color="#990000">.</font>

<b><font color="#000000">receive_data</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">MRef</font>, <font color="#009900">StreamRef</font>) <font color="#990000">-&gt;</font>
    <b><font color="#0000FF">receive</font></b>
        {<font color="#FF6600">gun_data</font>, <font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#FF6600">nofin</font>, <font color="#009900">Data</font>} <font color="#990000">-&gt;</font>
            <b><font color="#000000">io:format</font></b>(<font color="#FF0000">"~s~n"</font>, [<font color="#009900">Data</font>]),
            <b><font color="#000000">receive_data</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">MRef</font>, <font color="#009900">StreamRef</font>);
        {<font color="#FF6600">gun_data</font>, <font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>, <font color="#FF6600">fin</font>, <font color="#009900">Data</font>} <font color="#990000">-&gt;</font>
            <b><font color="#000000">io:format</font></b>(<font color="#FF0000">"~s~n"</font>, [<font color="#009900">Data</font>]);
        {<font color="#FF6600">'DOWN'</font>, <font color="#009900">MRef</font>, <b><font color="#000080">process</font></b>, <font color="#009900">ConnPid</font>, <font color="#009900">Reason</font>} <font color="#990000">-&gt;</font>
            <b><font color="#000000">error_logger:error_msg</font></b>(<font color="#FF0000">"Oops!"</font>),
            <b><font color="#000080">exit</font></b>(<font color="#009900">Reason</font>)
    <b><font color="#0000FF">after</font></b> <font color="#993399">1000</font> <font color="#990000">-&gt;</font>
        <b><font color="#000080">exit</font></b>(<font color="#FF6600">timeout</font>)
    <b><font color="#0000FF">end</font></b><font color="#990000">.</font></tt></pre>
</div></div>
<p>While it may seem verbose, using messages like this has the advantage of never locking your process, allowing you to easily debug your code. It also allows you to start more than one connection and concurrently perform queries on all of them at the same time.</p>
<p>You can also use Gun in a synchronous manner by using the <em>await</em> functions.</p>
<p>The <code>gun:await/2,3,4</code> function will wait until it receives a response to, a pushed resource related to, or data from the given stream.</p>
<p>When calling <code>gun:await/2,3</code> and not passing a monitor reference, one is automatically created for you for the duration of the call.</p>
<p>The <code>gun:await_body/2,3,4</code> works similarly, but returns the body received. Both functions can be combined to receive the response and its body sequentially.</p>
<div class="listingblock"><div class="title">Receiving a response using await</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:get</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/"</font>),
<b><font color="#0000FF">case</font></b> <b><font color="#000000">gun:await</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>) <b><font color="#0000FF">of</font></b>
    {<font color="#FF6600">response</font>, <font color="#FF6600">fin</font>, <font color="#009900">Status</font>, <font color="#009900">Headers</font>} <font color="#990000">-&gt;</font>
        <font color="#FF6600">no_data</font>;
    {<font color="#FF6600">response</font>, <font color="#FF6600">nofin</font>, <font color="#009900">Status</font>, <font color="#009900">Headers</font>} <font color="#990000">-&gt;</font>
        {<font color="#FF6600">ok</font>, <font color="#009900">Body</font>} <font color="#990000">=</font> <b><font color="#000000">gun:await_body</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">StreamRef</font>),
        <b><font color="#000000">io:format</font></b>(<font color="#FF0000">"~s~n"</font>, [<font color="#009900">Body</font>])
<b><font color="#0000FF">end</font></b><font color="#990000">.</font></tt></pre>
</div></div>
<h2 id="_handling_streams_pushed_by_the_server">Handling streams pushed by the server</h2>
<p>The HTTP/2 protocol allows the server to push more than one resource for every request. It will start sending those extra resources before it starts sending the response itself, so Gun will send you <code>gun_push</code> messages before <code>gun_response</code> when that happens.</p>
<p>You can safely choose to ignore <code>gun_push</code> messages, or you can handle them. If you do, you can either receive the messages directly or use <em>await</em> functions.</p>
<p>The <code>gun_push</code> message contains both the new stream reference and the stream reference of the original request.</p>
<div class="listingblock"><div class="title">Receiving a pushed response using receive</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">receive</font></b>
    {<font color="#FF6600">gun_push</font>, <font color="#009900">ConnPid</font>, <font color="#009900">OriginalStreamRef</font>, <font color="#009900">PushedStreamRef</font>,
            <font color="#009900">Method</font>, <font color="#009900">Host</font>, <font color="#009900">Path</font>, <font color="#009900">Headers</font>} <font color="#990000">-&gt;</font>
        <b><font color="#000000">enjoy</font></b>()
<b><font color="#0000FF">end</font></b><font color="#990000">.</font></tt></pre>
</div></div>
<p>If you use the <code>gun:await/2,3,4</code> function, however, Gun will use the original reference to identify the message but will return a tuple that doesn&apos;t contain it.</p>
<div class="listingblock"><div class="title">Receiving a pushed response using await</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>{<font color="#FF6600">push</font>, <font color="#009900">PushedStreamRef</font>, <font color="#009900">Method</font>, <font color="#009900">URI</font>, <font color="#009900">Headers</font>}
    <font color="#990000">=</font> <b><font color="#000000">gun:await</font></b>(<font color="#009900">ConnPid</font>, <font color="#009900">OriginalStreamRef</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<p>The <code>PushedStreamRef</code> variable can then be used with <code>gun:await/2,3,4</code> and <code>gun:await_body/2,3,4</code>.</p>
<h2 id="_flushing_unwanted_messages">Flushing unwanted messages</h2>
<p>Gun provides the function <code>gun:flush/1</code> to quickly get rid of unwanted messages sitting in the process mailbox. You can use it to get rid of all messages related to a connection, or just the messages related to a stream.</p>
<div class="listingblock"><div class="title">Flush all messages from a Gun connection</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">gun:flush</font></b>(<font color="#009900">ConnPid</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<div class="listingblock"><div class="title">Flush all messages from a specific stream</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">gun:flush</font></b>(<font color="#009900">StreamRef</font>)<font color="#990000">.</font></tt></pre>
</div></div>
<h2 id="_redirecting_responses_to_a_different_process">Redirecting responses to a different process</h2>
<p>Gun allows you to specify which process will handle responses to a request via the <code>reply_to</code> request option.</p>
<div class="listingblock"><div class="title">GET &quot;/organizations/ninenines&quot; to a different process</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#009900">StreamRef</font> <font color="#990000">=</font> <b><font color="#000000">gun:get</font></b>(<font color="#009900">ConnPid</font>, <font color="#FF0000">"/organizations/ninenines"</font>, [],
    #{<font color="#0000FF">reply_to</font> <font color="#990000">=&gt;</font> <font color="#009900">Pid</font>})<font color="#990000">.</font></tt></pre>
</div></div>




	
		
		
		
		
		

		<nav style="margin:1em 0">
			
				<a style="float:left" href="https://ninenines.eu/docs/en/gun/1.1/guide/connect/">
					Connection
				</a>
			

			
				<a style="float:right" href="https://ninenines.eu/docs/en/gun/1.1/guide/websocket/">
					Websocket
				</a>
			
		</nav>
	



</div>

<div class="span3 sidecol">


<h3>
	Gun
	1.1
	
	User Guide
</h3>

<ul>
	
		<li><a href="/docs/en/gun/1.1/guide">User Guide</a></li>
	
	
		<li><a href="/docs/en/gun/1.1/manual">Function Reference</a></li>
	
	
</ul>

<h4 id="docs-nav">Navigation</h4>

<h4>Version select</h4>
<ul>
	
	
	
		<li><a href="/docs/en/gun/2.0/guide">2.0</a></li>
	
		<li><a href="/docs/en/gun/1.3/guide">1.3</a></li>
	
		<li><a href="/docs/en/gun/1.2/guide">1.2</a></li>
	
		<li><a href="/docs/en/gun/1.1/guide">1.1</a></li>
	
		<li><a href="/docs/en/gun/1.0/guide">1.0</a></li>
	
</ul>

<h3 id="_like_my_work__donate">Like my work? Donate!</h3>
<p>Donate to Loïc Hoguin because his work on Cowboy, Ranch, Gun and Erlang.mk is fantastic:</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="display:inline">
<input type="hidden" name="cmd" value="_donations">
<input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="lc" value="FR">
<input type="hidden" name="item_name" value="Loic Hoguin">
<input type="hidden" name="item_number" value="99s">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="bn" value="PP-DonationsBF:btn_donate_LG.gif:NonHosted">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/fr_FR/i/scr/pixel.gif" width="1" height="1">
</form><p>Recurring payment options are also available via <a href="https://salt.bountysource.com/teams/ninenines">BountySource</a>. These funds are used to cover the recurring expenses like dedicated servers or domain names.</p>



</div>
</div>
</div>
</div>

      <footer>
        <div class="container">
          <div class="row">
            <div class="span6">
              <p id="scroll-top"><a href="#">↑ Scroll to top</a></p>
              <nav>
                <ul>
                  <li><a href="mailto:[email protected]" title="Contact us">Contact us</a></li><li><a href="https://github.com/ninenines/ninenines.github.io" title="Github repository">Contribute to this site</a></li>
                </ul>
              </nav>
            </div>
            <div class="span6 credits">
               <p><img src="/img/footer_logo.png"></p>
               <p>Copyright &copy; Loïc Hoguin 2012-2018</p>
            </div>
          </div>
        </div>
      </footer>

    
    <script src="/js/custom.js"></script>
  </body>
</html>