body_length(Req :: cowboy_req:req()) -> undefined | non_neg_integer()
From 13fd7a716c82f384f42f4dfb6c3e8a6b90f72054 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= cowboy_req:body_length - Body length Return the length of the request body. The length is not always known before reading the body.
+In those cases Cowboy will return
+The Req object.
+ The length of the request body, or
+2.0: Only the length is returned, it is no longer wrapped in a tuple.
+
+1.0: Function introduced.
+ cowboy_req:has_body - Is there a request body? Return whether the request has a body.
+The Req object.
+ A boolean indicating whether the request has a body.
+1.0: Function introduced.
+ cowboy_req:read_body - Read the request body Read the request body. This function reads a chunk of the request body. A An This function reads the request body from the connection process.
+The connection process is responsible for reading from the socket.
+The exact behavior varies depending on the protocol. The options therefore are only related to the communication
+between the request process and the connection process. Cowboy will automatically handle protocol details including
+the expect header, chunked transfer-encoding and others. Once the body has been read fully, Cowboy sets the content-length
+header if it was not previously provided.
+The Req object.
+
+A map of body reading options.
+ The The The connection process sends data to the request process when
+either the The A An The data is always returned as a binary. The Req object returned in the tuple must be used for that point
+onward. It contains a more up to date representation of the request.
+For example it may have an added content-length header once the
+body has been read.
+2.0: Function introduced. Replaces cowboy_req:read_part - Read the next multipart headers Read the next part of a multipart body. This function reads the request body and parses it as
+multipart. Each parts of a multipart representation have
+their own headers and body. This function parses and returns
+headers. Examples of multipart media types are
+ Cowboy will skip any data remaining until the beginning of
+the next part. This includes the preamble to the multipart
+message but also the body of a previous part if it hasn’t
+been read. Both are skipped automatically when calling this
+function. Cowboy will read the body before parsing in chunks of size
+up to 64KB, with a period of 5 seconds. This is tailored for
+reading part headers and might not be the most efficient for
+skipping the previous part’s body. The headers returned are MIME headers, NOT HTTP headers.
+They can be parsed using the functions from the Once a part has been read, it can not be read again. Once the body has been read, Cowboy sets the content-length
+header if it was not previously provided.
+The Req object.
+
+A map of body reading options. Please refer to
+cowboy_req:read_body(3)
+for details about each option.
+ This function defaults the An A The Req object returned in the tuple must be used for that point
+onward. It contains a more up to date representation of the request.
+For example it may have an added content-length header once the
+body has been read.
+2.0: Function introduced. Replaces cowboy_req:read_part_body - Read the current part’s body Read the body of the current part of the multipart message. This function reads the request body and parses it as
+multipart. Each parts of a multipart representation have
+their own headers and body. This function returns the
+body of the current part. Examples of multipart media types
+are This function reads a chunk of the part’s body. A Once a part has been read, it can not be read again. Once the body has been read, Cowboy sets the content-length
+header if it was not previously provided.
+The Req object.
+
+A map of body reading options. Please refer to
+cowboy_req:read_body(3)
+for details about each option.
+ This function uses the same default options as the
+cowboy_req:read_body(3)
+function. A An The data is always returned as a binary. The Req object returned in the tuple must be used for that point
+onward. It contains a more up to date representation of the request.
+For example it may have an added content-length header once the
+body has been read.
+2.0: Function introduced. Replaces cowboy_req:read_urlencoded_body - Read and parse a urlencoded request body Read and parse a urlencoded request body. This function reads the request body and parses it as
+ The urlencoded media type is used by Web browsers when
+submitting HTML forms using the POST method. Cowboy needs to read the full body before parsing. By default
+it will read bodies of size up to 64KB. It is possible to
+provide options to read larger bodies if required. Cowboy will automatically handle protocol details including
+the expect header, chunked transfer-encoding and others. Once the body has been read, Cowboy sets the content-length
+header if it was not previously provided. This function can only be called once. Calling it again will
+result in undefined behavior.
+The Req object.
+
+A map of body reading options. Please refer to
+cowboy_req:read_body(3)
+for details about each option.
+ This function defaults the An The Req object returned in the tuple must be used for that point
+onward. It contains a more up to date representation of the request.
+For example it may have an added content-length header once the
+body has been read.
+2.0: Function introduced. Replaces 99s
+ cowboy_req:body_length(3)
+
+Name
+Description
+body_length(Req :: cowboy_req:req()) -> undefined | non_neg_integer()
undefined
. The body
+length is available after the body has been fully read.Arguments
+
+
Return value
+undefined
if it is
+not known.Changelog
+
+
Examples
+Length = cowboy_req:body_length(Req).
+ Cowboy
+ 2.0
+ Function Reference
+
+
+
+
+
+
+
+Navigation
+
+Version select
+
+
+99s
+ cowboy_req:has_body(3)
+
+Name
+Description
+has_body(Req :: cowboy_req:req()) -> boolean()
Arguments
+
+
Return value
+Changelog
+
+
Examples
+true = cowboy_req:has_body(Req).
+ Cowboy
+ 2.0
+ Function Reference
+
+
+
+
+
+
+
+Navigation
+
+Version select
+
+
+99s
+ cowboy_req:read_body(3)
+
+Name
+Description
+read_body(Req :: cowboy_req:req())
+ -> read_body(Req, #{})
+
+read_body(Req :: cowboy_req:req(), Opts)
+ -> {ok, Data :: binary(), Req}
+ | {more, Data :: binary(), Req}
+
+Opts :: cowboy_req:read_body_opts()
more
tuple
+is returned when more data remains to be read. Call the function
+repeatedly until an ok
tuple is returned to read the entire body.ok
tuple with empty data is returned when the request has no body,
+or when calling this function again after the body has already
+been read. It is therefore safe to call this function directly.
+Note that the body can only be read once.Arguments
+
+
length
option can be used to request smaller or bigger
+chunks of data to be sent. It is a best effort approach, Cowboy
+may send more data than configured on occasions. It defaults
+to 8MB.period
indicates how long the connection process will wait
+before it provides us with the data it received. It defaults
+to 15 seconds.length
of data or the period
of time is reached.timeout
option is a safeguard in case the connection
+process becomes unresponsive. The function will crash if no
+message was received in that interval. The timeout should be
+larger than the period. It defaults to the period + 1 second.Return value
+more
tuple is returned when there are more data to be read.ok
tuple is returned when there are no more data to be read,
+either because this is the last chunk of data, the body has already
+been read, or there was no body to begin with.Changelog
+
+
body/1,2
.
+Examples
+read_body(Req0, Acc) ->
+ case cowboy_req:read_body(Req0) of
+ {ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req};
+ {more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>)
+ end.
cowboy_req:read_body(Req, #{length => 64000}).
+ Cowboy
+ 2.0
+ Function Reference
+
+
+
+
+
+
+
+Navigation
+
+Version select
+
+
+99s
+ cowboy_req:read_part(3)
+
+Name
+Description
+read_part(Req :: cowboy_req:req())
+ -> read_part(Req, #{})
+
+read_part(Req :: cowboy_req:req(), Opts)
+ -> {ok, Headers, Req} | {done, Req}
+
+Opts :: cowboy_req:read_body_opts()
+Headers :: cow_multipart:headers()
multipart/form-data
and multipart/byteranges
.cow_multipart
+module. In addition, the cow_multipart:form_data/1
function
+can be used to quickly extract information from multipart/form-data
+representations.Arguments
+
+
length
to 64KB and the period
+to 5 seconds.Return value
+ok
tuple is returned containing the next part’s headers
+as a list of key/values.done
tuple is returned if there are no more parts to read.Changelog
+
+
part/1,2
.
+Examples
+acc_multipart(Req0, Acc) ->
+ case cowboy_req:read_part(Req0) of
+ {ok, Headers, Req1} ->
+ {ok, Body, Req} = stream_body(Req1, <<>>),
+ acc_multipart(Req, [{Headers, Body}|Acc]);
+ {done, Req} ->
+ {lists:reverse(Acc), Req}
+ end.
+
+stream_body(Req0, Acc) ->
+ case cowboy_req:read_part_body(Req0) of
+ {more, Data, Req} ->
+ stream_body(Req, << Acc/binary, Data/binary >>);
+ {ok, Data, Req} ->
+ {ok, << Acc/binary, Data/binary >>, Req}
+ end.
skip_body_multipart(Req0, Acc) ->
+ case cowboy_req:read_part(Req0) of
+ {ok, Headers, Req} ->
+ skip_body_multipart(Req, [Headers|Acc]);
+ {done, Req} ->
+ {lists:reverse(Acc), Req}
+ end.
{ok, Headers, Req} = cowboy_req:read_part(Req0, #{length => 1000000}).
+ Cowboy
+ 2.0
+ Function Reference
+
+
+
+
+
+
+
+Navigation
+
+Version select
+
+
+99s
+ cowboy_req:read_part_body(3)
+
+Name
+Description
+read_part_body(Req :: cowboy_req:req())
+ -> read_part_body(Req, #{})
+
+read_part_body(Req :: cowboy_req:req(), Opts)
+ -> {ok, Data :: binary(), Req}
+ | {more, Data :: binary(), Req}
+
+Opts :: cowboy_req:read_body_opts()
multipart/form-data
and multipart/byteranges
.more
tuple
+is returned when more data remains to be read. Call the function
+repeatedly until an ok
tuple is returned to read the entire body.Arguments
+
+
Return value
+more
tuple is returned when there are more data to be read.ok
tuple is returned when there are no more data to be read.Changelog
+
+
part_body/1,2
.
+Examples
+stream_body(Req0, Acc) ->
+ case cowboy_req:read_part_body(Req0) of
+ {more, Data, Req} ->
+ stream_body(Req, << Acc/binary, Data/binary >>);
+ {ok, Data, Req} ->
+ {ok, << Acc/binary, Data/binary >>, Req}
+ end.
{ok, Body, Req} = cowboy_req:read_part_body(Req0, #{length => 64000}).
+ Cowboy
+ 2.0
+ Function Reference
+
+
+
+
+
+
+
+Navigation
+
+Version select
+
+
+99s
+ cowboy_req:read_urlencoded_body(3)
+
+Name
+Description
+read_urlencoded_body(Req :: cowboy_req:req())
+ -> read_urlencoded_body(Req, #{})
+
+read_urlencoded_body(Req :: cowboy_req:req(), Opts)
+ -> {ok, Body, Req}
+
+Opts :: cowboy_req:read_body_opts()
+Body :: [{Key :: binary(), Value :: binary() | true}]
application/x-www-form-urlencoded
. It returns a list
+of key/values.Arguments
+
+
length
to 64KB and the period
+to 5 seconds.Return value
+ok
tuple is returned containing a list of key/values found
+in the body.Changelog
+
+
body_qs/1,2
.
+Examples
+{ok, Body, Req} = cowboy_req:read_urlencoded_body(Req0),
+{_, Lang} = lists:keyfind(<<"lang">>, 1, Body).
{ok, Body, Req} = cowboy_req:read_urlencoded_body(Req0, #{length => 1000000}).
+ Cowboy
+ 2.0
+ Function Reference
+
+
+
+
+
+
+
+Navigation
+
+Version select
+
+
+
-cowboy_req:read_part(3) - Read the next part of a multipart body +cowboy_req:read_part(3) - Read the next multipart headers
-cowboy_req:read_part_body(3) - Read the current part’s body in a multipart body +cowboy_req:read_part_body(3) - Read the current part’s body