diff options
author | Loïc Hoguin <[email protected]> | 2018-04-04 13:13:37 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2018-04-04 13:13:37 +0200 |
commit | d2a3f2cedd7c00d0933222aed9c06b3149aa4db4 (patch) | |
tree | 4f1499bac639c782b250af0fbf0dec2064315813 /docs/en/cowboy/2.3/guide/req_body/index.html | |
parent | 1d654719f5fa6be67d2c95145872068665702cb7 (diff) | |
download | ninenines.eu-d2a3f2cedd7c00d0933222aed9c06b3149aa4db4.tar.gz ninenines.eu-d2a3f2cedd7c00d0933222aed9c06b3149aa4db4.tar.bz2 ninenines.eu-d2a3f2cedd7c00d0933222aed9c06b3149aa4db4.zip |
Cowboy 2.3.0
Diffstat (limited to 'docs/en/cowboy/2.3/guide/req_body/index.html')
-rw-r--r-- | docs/en/cowboy/2.3/guide/req_body/index.html | 305 |
1 files changed, 305 insertions, 0 deletions
diff --git a/docs/en/cowboy/2.3/guide/req_body/index.html b/docs/en/cowboy/2.3/guide/req_body/index.html new file mode 100644 index 00000000..7d05b693 --- /dev/null +++ b/docs/en/cowboy/2.3/guide/req_body/index.html @@ -0,0 +1,305 @@ +<!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"> + + <meta name="generator" content="Hugo 0.37.1" /> + + <title>Nine Nines: Reading the request body</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=1" 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="Keep in touch!" href="http://twitter.com/lhoguin"><img src="/img/ico_microblog.png" data-hover="/img/ico_microblog_alt.png"></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>Reading the request body</span></h1> + +<div class="paragraph"><p>The request body can be read using the Req object.</p></div> +<div class="paragraph"><p>Cowboy will not attempt to read the body until requested. +You need to call the body reading functions in order to +retrieve it.</p></div> +<div class="paragraph"><p>Cowboy will not cache the body, it is therefore only +possible to read it once.</p></div> +<div class="paragraph"><p>You are not required to read it, however. If a body is +present and was not read, Cowboy will either cancel or +skip its download, depending on the protocol.</p></div> +<div class="paragraph"><p>Cowboy provides functions for reading the body raw, +and read and parse form urlencoded or <a href="../multipart">multipart bodies</a>. +The latter is covered in its own chapter.</p></div> +<div class="sect1"> +<h2 id="_request_body_presence">Request body presence</h2> +<div class="sectionbody"> +<div class="paragraph"><p>Not all requests come with a body. You can check for +the presence of a request body with this function:</p></div> +<div class="listingblock"> +<div class="content"><!-- Generator: GNU source-highlight +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt><span style="font-weight: bold"><span style="color: #000000">cowboy_req:has_body</span></span>(<span style="color: #009900">Req</span>)<span style="color: #990000">.</span></tt></pre></div></div> +<div class="paragraph"><p>It returns <code>true</code> if there is a body; <code>false</code> otherwise.</p></div> +<div class="paragraph"><p>In practice, this function is rarely used. When the +method is <code>POST</code>, <code>PUT</code> or <code>PATCH</code>, the request body +is often required by the application, which should +just attempt to read it directly.</p></div> +</div> +</div> +<div class="sect1"> +<h2 id="_request_body_length">Request body length</h2> +<div class="sectionbody"> +<div class="paragraph"><p>You can obtain the length of the body:</p></div> +<div class="listingblock"> +<div class="content"><!-- Generator: GNU source-highlight +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt><span style="color: #009900">Length</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">cowboy_req:body_length</span></span>(<span style="color: #009900">Req</span>)<span style="color: #990000">.</span></tt></pre></div></div> +<div class="paragraph"><p>Note that the length may not be known in advance. In +that case <code>undefined</code> will be returned. This can happen +with HTTP/1.1’s chunked transfer-encoding, or HTTP/2 +when no content-length was provided.</p></div> +<div class="paragraph"><p>Cowboy will update the body length in the Req object +once the body has been read completely. A length will +always be returned when attempting to call this function +after reading the body completely.</p></div> +</div> +</div> +<div class="sect1"> +<h2 id="_reading_the_body">Reading the body</h2> +<div class="sectionbody"> +<div class="paragraph"><p>You can read the entire body with one function call:</p></div> +<div class="listingblock"> +<div class="content"><!-- Generator: GNU source-highlight +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt>{<span style="color: #FF6600">ok</span>, <span style="color: #009900">Data</span>, <span style="color: #009900">Req</span>} <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">cowboy_req:read_body</span></span>(<span style="color: #009900">Req0</span>)<span style="color: #990000">.</span></tt></pre></div></div> +<div class="paragraph"><p>Cowboy returns an <code>ok</code> tuple when the body has been +read fully.</p></div> +<div class="paragraph"><p>By default, Cowboy will attempt to read up to 8MB +of data, for up to 15 seconds. The call will return +once Cowboy has read at least 8MB of data, or at +the end of the 15 seconds period.</p></div> +<div class="paragraph"><p>These values can be customized. For example, to read +only up to 1MB for up to 5 seconds:</p></div> +<div class="listingblock"> +<div class="content"><!-- Generator: GNU source-highlight +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt>{<span style="color: #FF6600">ok</span>, <span style="color: #009900">Data</span>, <span style="color: #009900">Req</span>} <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">cowboy_req:read_body</span></span>(<span style="color: #009900">Req0</span>, + #{<span style="font-weight: bold"><span style="color: #000080">length</span></span> <span style="color: #990000">=></span> <span style="color: #993399">1000000</span>, <span style="color: #0000FF">period</span> <span style="color: #990000">=></span> <span style="color: #993399">5000</span>})<span style="color: #990000">.</span></tt></pre></div></div> +<div class="paragraph"><p>You may also disable the length limit:</p></div> +<div class="listingblock"> +<div class="content"><!-- Generator: GNU source-highlight +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt>{<span style="color: #FF6600">ok</span>, <span style="color: #009900">Data</span>, <span style="color: #009900">Req</span>} <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">cowboy_req:read_body</span></span>(<span style="color: #009900">Req0</span>, #{<span style="font-weight: bold"><span style="color: #000080">length</span></span> <span style="color: #990000">=></span> <span style="color: #FF6600">infinity</span>})<span style="color: #990000">.</span></tt></pre></div></div> +<div class="paragraph"><p>This makes the function wait 15 seconds and return with +whatever arrived during that period. This is not +recommended for public facing applications.</p></div> +<div class="paragraph"><p>These two options can effectively be used to control +the rate of transmission of the request body.</p></div> +</div> +</div> +<div class="sect1"> +<h2 id="_streaming_the_body">Streaming the body</h2> +<div class="sectionbody"> +<div class="paragraph"><p>When the body is too large, the first call will return +a <code>more</code> tuple instead of <code>ok</code>. You can call the +function again to read more of the body, reading +it one chunk at a time.</p></div> +<div class="listingblock"> +<div class="content"><!-- Generator: GNU source-highlight +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt><span style="font-weight: bold"><span style="color: #000000">read_body_to_console</span></span>(<span style="color: #009900">Req0</span>) <span style="color: #990000">-></span> + <span style="font-weight: bold"><span style="color: #0000FF">case</span></span> <span style="font-weight: bold"><span style="color: #000000">cowboy_req:read_body</span></span>(<span style="color: #009900">Req0</span>) <span style="font-weight: bold"><span style="color: #0000FF">of</span></span> + {<span style="color: #FF6600">ok</span>, <span style="color: #009900">Data</span>, <span style="color: #009900">Req</span>} <span style="color: #990000">-></span> + <span style="font-weight: bold"><span style="color: #000000">io:format</span></span>(<span style="color: #FF0000">"~s"</span>, [<span style="color: #009900">Data</span>]), + <span style="color: #009900">Req</span>; + {<span style="color: #FF6600">more</span>, <span style="color: #009900">Data</span>, <span style="color: #009900">Req</span>} <span style="color: #990000">-></span> + <span style="font-weight: bold"><span style="color: #000000">io:format</span></span>(<span style="color: #FF0000">"~s"</span>, [<span style="color: #009900">Data</span>]), + <span style="font-weight: bold"><span style="color: #000000">read_body_to_console</span></span>(<span style="color: #009900">Req</span>) + <span style="font-weight: bold"><span style="color: #0000FF">end</span></span><span style="color: #990000">.</span></tt></pre></div></div> +<div class="paragraph"><p>The <code>length</code> and <code>period</code> options can also be used. +They need to be passed for every call.</p></div> +</div> +</div> +<div class="sect1"> +<h2 id="_reading_a_form_urlencoded_body">Reading a form urlencoded body</h2> +<div class="sectionbody"> +<div class="paragraph"><p>Cowboy provides a convenient function for reading and +parsing bodies sent as application/x-www-form-urlencoded.</p></div> +<div class="listingblock"> +<div class="content"><!-- Generator: GNU source-highlight +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt>{<span style="color: #FF6600">ok</span>, <span style="color: #009900">KeyValues</span>, <span style="color: #009900">Req</span>} <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">cowboy_req:read_urlencoded_body</span></span>(<span style="color: #009900">Req0</span>)<span style="color: #990000">.</span></tt></pre></div></div> +<div class="paragraph"><p>This function returns a list of key/values, exactly like +the function <code>cowboy_req:parse_qs/1</code>.</p></div> +<div class="paragraph"><p>The defaults for this function are different. Cowboy will +read for up to 64KB and up to 5 seconds. They can be modified:</p></div> +<div class="listingblock"> +<div class="content"><!-- Generator: GNU source-highlight +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><tt>{<span style="color: #FF6600">ok</span>, <span style="color: #009900">KeyValues</span>, <span style="color: #009900">Req</span>} <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">cowboy_req:read_urlencoded_body</span></span>(<span style="color: #009900">Req0</span>, + #{<span style="font-weight: bold"><span style="color: #000080">length</span></span> <span style="color: #990000">=></span> <span style="color: #993399">4096</span>, <span style="color: #0000FF">period</span> <span style="color: #990000">=></span> <span style="color: #993399">3000</span>})<span style="color: #990000">.</span></tt></pre></div></div> +</div> +</div> + + + + + + + + + + + <nav style="margin:1em 0"> + + <a style="float:left" href="https://ninenines.eu/docs/en/cowboy/2.3/guide/req/"> + The Req object + </a> + + + + <a style="float:right" href="https://ninenines.eu/docs/en/cowboy/2.3/guide/resp/"> + Sending a response + </a> + + </nav> + + + + +</div> + +<div class="span3 sidecol"> + + +<h3> + Cowboy + 2.3 + + User Guide +</h3> + +<ul> + + <li><a href="/docs/en/cowboy/2.3/guide">User Guide</a></li> + + + <li><a href="/docs/en/cowboy/2.3/manual">Function Reference</a></li> + + +</ul> + +<h4 id="docs-nav">Navigation</h4> + +<h4>Version select</h4> +<ul> + + + + <li><a href="/docs/en/cowboy/2.3/guide">2.3</a></li> + + <li><a href="/docs/en/cowboy/2.2/guide">2.2</a></li> + + <li><a href="/docs/en/cowboy/2.1/guide">2.1</a></li> + + <li><a href="/docs/en/cowboy/2.0/guide">2.0</a></li> + + <li><a href="/docs/en/cowboy/1.0/guide">1.0</a></li> + +</ul> + +</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 © Loïc Hoguin 2012-2016</p> + </div> + </div> + </div> + </footer> + + + <script src="/js/custom.js"></script> + </body> +</html> + + |