Debian Codegraph @

doc/src/manual/cowboy_req.read_and_match_urlencoded_body.asciidoc

raw
1 = cowboy_req:read_and_match_urlencoded_body(3)
2
3 == Name
4
5 cowboy_req:read_and_match_urlencoded_body - Read, parse
6 and match a urlencoded request body against constraints
7
8 == Description
9
10 [source,erlang]
11 ----
12 read_and_match_urlencoded_body(Fields, Req)
13 -> read_and_match_urlencoded_body(Fields, Req, #{})
14
15 read_and_match_urlencoded_body(Fields, Req, Opts)
16 -> {ok, Body, Req}
17
18 Fields :: cowboy:fields()
19 Req :: cowboy_req:req()
20 Opts :: cowboy_req:read_body_opts()
21 Body :: #{atom() => any()}
22 ----
23
24 Read, parse and match a urlencoded request body against
25 constraints.
26
27 This function reads the request body and parses it as
28 `application/x-www-form-urlencoded`. It then applies
29 the given field constraints to the urlencoded data
30 and returns the result as a map.
31
32 The urlencoded media type is used by Web browsers when
33 submitting HTML forms using the POST method.
34
35 Cowboy will only return the values specified
36 in the fields list, and ignore all others. Fields can be
37 either the key requested; the key along with a list of
38 constraints; or the key, a list of constraints and a
39 default value in case the key is missing.
40
41 This function will crash if the key is missing and no
42 default value is provided. This function will also crash
43 if a constraint fails.
44
45 The key must be provided as an atom. The key of the
46 returned map will be that atom. The value may be converted
47 through the use of constraints, making this function able
48 to extract, validate and convert values all in one step.
49
50 Cowboy needs to read the full body before parsing. By default
51 it will read bodies of size up to 64KB. It is possible to
52 provide options to read larger bodies if required.
53
54 Cowboy will automatically handle protocol details including
55 the expect header, chunked transfer-encoding and others.
56
57 Once the body has been read, Cowboy sets the content-length
58 header if it was not previously provided.
59
60 This function can only be called once. Calling it again will
61 result in undefined behavior.
62
63 == Arguments
64
65 Fields::
66
67 Fields to retrieve from the urlencoded body.
68 +
69 See link:man:cowboy(3)[cowboy(3)] for a complete description.
70
71 Req::
72
73 The Req object.
74
75 Opts::
76
77 A map of body reading options. Please refer to
78 link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)]
79 for details about each option.
80 +
81 This function defaults the `length` to 64KB, the `period`
82 to 5 seconds and `max_keys` to 100.
83
84 == Return value
85
86 An `ok` tuple is returned.
87
88 Desired values are returned as a map. The key is the atom
89 that was given in the list of fields, and the value is the
90 optionally converted value after applying constraints.
91
92 The map contains the same keys that were given in the fields.
93
94 An exception is triggered when the match fails.
95
96 The Req object returned in the tuple must be used from that point
97 onward. It contains a more up to date representation of the request.
98 For example it may have an added content-length header once the
99 body has been read.
100
101 == Changelog
102
103 * *2.17*: Option `max_keys` added.
104 * *2.5*: Function introduced.
105
106 == Examples
107
108 .Match fields
109 [source,erlang]
110 ----
111 %% ID and Lang are binaries.
112 #{id := ID, lang := Lang}
113 = cowboy_req:read_and_match_urlencoded_body(
114 [id, lang], Req).
115 ----
116
117 .Match fields and apply constraints
118 [source,erlang]
119 ----
120 %% ID is an integer and Lang a non-empty binary.
121 #{id := ID, lang := Lang}
122 = cowboy_req:read_and_match_urlencoded_body(
123 [{id, int}, {lang, nonempty}], Req).
124 ----
125
126 .Match fields with default values
127 [source,erlang]
128 ----
129 #{lang := Lang}
130 = cowboy_req:read_and_match_urlencoded_body(
131 [{lang, [], <<"en-US">>}], Req).
132 ----
133
134 .Allow large urlencoded bodies
135 [source,erlang]
136 ----
137 {ok, Body, Req} = cowboy_req:read_and_match_urlencoded_body(
138 Fields, Req0, #{length => 1000000}).
139 ----
140
141 == See also
142
143 link:man:cowboy_req(3)[cowboy_req(3)],
144 link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)],
145 link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)],
146 link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)],
147 link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)],
148 link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)],
149 link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)]