cowboy_req.uri.asciidoc 2.8 KB

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