Debian Codegraph @

doc/src/manual/cowboy_req.uri.asciidoc

raw
1 = cowboy_req:uri(3)
2
3 == Name
4
5 cowboy_req:uri - Reconstructed URI
6
7 == Description
8
9 [source,erlang]
10 ----
11 uri(Req :: cowboy_req:req()) -> uri(Req, #{})
12 uri(Req :: cowboy_req:req(), Opts) -> URI :: iodata()
13
14 Opts :: #{
15 scheme => iodata() | undefined,
16 host => iodata() | undefined,
17 port => inet:port_number() | undefined,
18 path => iodata() | undefined,
19 qs => iodata() | undefined,
20 fragment => iodata() | undefined
21 }
22 ----
23
24 Reconstruct the effective request URI, optionally modifying components.
25
26 By default Cowboy will build a URI using the components found
27 in the request. Options allow disabling or replacing individual
28 components.
29
30 == Arguments
31
32 Req::
33
34 The Req object.
35
36 Opts::
37
38 Map for overriding individual components.
39 +
40 To replace a component, provide its new value as a binary
41 string or an iolist. To disable a component, set its value
42 to `undefined`.
43 +
44 As this function always returns a valid URI, there are some
45 things to note:
46 +
47 * Disabling the host also disables the scheme and port.
48 * There is no fragment component by default as these are
49 not sent with the request.
50 * The port number may not appear in the resulting URI if
51 it is the default port for the given scheme (http: 80; https: 443).
52
53 == Return value
54
55 The reconstructed URI is returned as an iolist or a binary string.
56
57 == Changelog
58
59 * *2.0*: Individual components can be replaced or disabled.
60 * *2.0*: Only the URI is returned, it is no longer wrapped in a tuple.
61 * *2.0*: Function introduced. Replaces `host_url/1` and `url/1`.
62
63 == Examples
64
65 With an effective request URI http://example.org/path/to/res?edit=1
66 we can have:
67
68 .Protocol relative form
69 [source,erlang]
70 ----
71 %% //example.org/path/to/res?edit=1
72 cowboy_req:uri(Req, #{scheme => undefined}).
73 ----
74
75 .Serialized origin for use in the origin header
76 [source,erlang]
77 ----
78 %% http://example.org
79 cowboy_req:uri(Req, #{path => undefined, qs => undefined}).
80 ----
81
82 .HTTP/1.1 origin form (path and query string only)
83 [source,erlang]
84 ----
85 %% /path/to/res?edit=1
86 cowboy_req:uri(Req, #{host => undefined}).
87 ----
88
89 .Add a fragment to the URI
90 [source,erlang]
91 ----
92 %% http://example.org/path/to/res?edit=1#errors
93 cowboy_req:uri(Req, #{fragment => <<"errors">>}).
94 ----
95
96 .Ensure the scheme is HTTPS
97 [source,erlang]
98 ----
99 %% https://example.org/path/to/res?edit=1
100 cowboy_req:uri(Req, #{scheme => <<"https">>}).
101 ----
102
103 .Convert the URI to a binary string
104 [source,erlang]
105 ----
106 iolist_to_binary(cowboy_req:uri(Req)).
107 ----
108
109 == See also
110
111 link:man:cowboy_req(3)[cowboy_req(3)],
112 link:man:cowboy_req:scheme(3)[cowboy_req:scheme(3)],
113 link:man:cowboy_req:host(3)[cowboy_req:host(3)],
114 link:man:cowboy_req:port(3)[cowboy_req:port(3)],
115 link:man:cowboy_req:path(3)[cowboy_req:path(3)],
116 link:man:cowboy_req:qs(3)[cowboy_req:qs(3)]