cowboy_req.asciidoc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. = cowboy_req(3)
  2. == Name
  3. cowboy_req - HTTP request and response
  4. == Description
  5. The module `cowboy_req` provides functions to access, manipulate
  6. and respond to requests.
  7. There are four types of functions in this module. They can be
  8. differentiated by their name and their return type:
  9. [options="header"]
  10. |===
  11. | Type | Name pattern | Return type
  12. | access | no verb, parse_*, match_* | `Value`
  13. | question | has_* | `boolean()`
  14. | modification | set_* | `Req`
  15. | action | any other verb | `ok \| {Result, Value, Req}`
  16. |===
  17. Any `Req` returned must be used in place of the one passed as
  18. argument. Functions that perform an action in particular write
  19. state in the Req object to make sure you are using the function
  20. correctly. For example, it's only possible to send one response,
  21. and to read the body once.
  22. == Exports
  23. Connection:
  24. * link:man:cowboy_req:peer(3)[cowboy_req:peer(3)] - Peer address and port
  25. * link:man:cowboy_req:sock(3)[cowboy_req:sock(3)] - Socket address and port
  26. * link:man:cowboy_req:cert(3)[cowboy_req:cert(3)] - Client TLS certificate
  27. Raw request:
  28. * link:man:cowboy_req:method(3)[cowboy_req:method(3)] - HTTP method
  29. * link:man:cowboy_req:version(3)[cowboy_req:version(3)] - HTTP version
  30. * link:man:cowboy_req:scheme(3)[cowboy_req:scheme(3)] - URI scheme
  31. * link:man:cowboy_req:host(3)[cowboy_req:host(3)] - URI host name
  32. * link:man:cowboy_req:port(3)[cowboy_req:port(3)] - URI port number
  33. * link:man:cowboy_req:path(3)[cowboy_req:path(3)] - URI path
  34. * link:man:cowboy_req:qs(3)[cowboy_req:qs(3)] - URI query string
  35. * link:man:cowboy_req:uri(3)[cowboy_req:uri(3)] - Reconstructed URI
  36. * link:man:cowboy_req:header(3)[cowboy_req:header(3)] - HTTP header
  37. * link:man:cowboy_req:headers(3)[cowboy_req:headers(3)] - HTTP headers
  38. Processed request:
  39. * link:man:cowboy_req:parse_qs(3)[cowboy_req:parse_qs(3)] - Parse the query string
  40. * link:man:cowboy_req:match_qs(3)[cowboy_req:match_qs(3)] - Match the query string against constraints
  41. * link:man:cowboy_req:parse_header(3)[cowboy_req:parse_header(3)] - Parse the given HTTP header
  42. * link:man:cowboy_req:parse_cookies(3)[cowboy_req:parse_cookies(3)] - Parse cookie headers
  43. * link:man:cowboy_req:match_cookies(3)[cowboy_req:match_cookies(3)] - Match cookies against constraints
  44. * link:man:cowboy_req:binding(3)[cowboy_req:binding(3)] - Access a value bound from the route
  45. * link:man:cowboy_req:bindings(3)[cowboy_req:bindings(3)] - Access all values bound from the route
  46. * link:man:cowboy_req:host_info(3)[cowboy_req:host_info(3)] - Access the route's heading host segments
  47. * link:man:cowboy_req:path_info(3)[cowboy_req:path_info(3)] - Access the route's trailing path segments
  48. Request body:
  49. * link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)] - Is there a request body?
  50. * link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)] - Body length
  51. * link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)] - Read the request body
  52. * link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)] - Read and parse a urlencoded request body
  53. * link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)] - Read the next multipart headers
  54. * link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)] - Read the current part's body
  55. Response:
  56. * link:man:cowboy_req:set_resp_cookie(3)[cowboy_req:set_resp_cookie(3)] - Set a cookie
  57. * link:man:cowboy_req:set_resp_header(3)[cowboy_req:set_resp_header(3)] - Set a response header
  58. * link:man:cowboy_req:set_resp_headers(3)[cowboy_req:set_resp_headers(3)] - Set several response headers
  59. * link:man:cowboy_req:has_resp_header(3)[cowboy_req:has_resp_header(3)] - Is the given response header set?
  60. * link:man:cowboy_req:resp_header(3)[cowboy_req:resp_header(3)] - Response header
  61. * link:man:cowboy_req:resp_headers(3)[cowboy_req:resp_headers(3)] - Response headers
  62. * link:man:cowboy_req:delete_resp_header(3)[cowboy_req:delete_resp_header(3)] - Delete a response header
  63. * link:man:cowboy_req:set_resp_body(3)[cowboy_req:set_resp_body(3)] - Set the response body
  64. * link:man:cowboy_req:has_resp_body(3)[cowboy_req:has_resp_body(3)] - Is there a response body?
  65. * link:man:cowboy_req:inform(3)[cowboy_req:inform(3)] - Send an informational response
  66. * link:man:cowboy_req:reply(3)[cowboy_req:reply(3)] - Send the response
  67. * link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)] - Send the response headers
  68. * link:man:cowboy_req:stream_body(3)[cowboy_req:stream_body(3)] - Stream the response body
  69. * link:man:cowboy_req:stream_events(3)[cowboy_req:stream_events(3)] - Stream events
  70. * link:man:cowboy_req:stream_trailers(3)[cowboy_req:stream_trailers(3)] - Send the response trailers
  71. * link:man:cowboy_req:push(3)[cowboy_req:push(3)] - Push a resource to the client
  72. == Types
  73. === push_opts()
  74. [source,erlang]
  75. ----
  76. push_opts() :: #{
  77. method => binary(), %% case sensitive
  78. scheme => binary(), %% lowercase; case insensitive
  79. host => binary(), %% lowercase; case insensitive
  80. port => inet:port_number(),
  81. qs => binary() %% case sensitive
  82. }
  83. ----
  84. Push options.
  85. By default, Cowboy will use the GET method, an empty query string,
  86. and take the scheme, host and port directly from the current
  87. request's URI.
  88. === read_body_opts()
  89. [source,erlang]
  90. ----
  91. read_body_opts() :: #{
  92. length => non_neg_integer(),
  93. period => non_neg_integer(),
  94. timeout => timeout()
  95. }
  96. ----
  97. Body reading options.
  98. The defaults are function-specific.
  99. === req()
  100. [source,erlang]
  101. ----
  102. req() :: #{
  103. method := binary(), %% case sensitive
  104. version := cowboy:http_version() | atom(),
  105. scheme := binary(), %% lowercase; case insensitive
  106. host := binary(), %% lowercase; case insensitive
  107. port := inet:port_number(),
  108. path := binary(), %% case sensitive
  109. qs := binary(), %% case sensitive
  110. headers := cowboy:http_headers(),
  111. peer := {inet:ip_address(), inet:port_number()},
  112. sock := {inet:ip_address(), inet:port_number()},
  113. cert := binary() | undefined
  114. }
  115. ----
  116. The Req object.
  117. Contains information about the request and response. While
  118. some fields are publicly documented, others aren't and shouldn't
  119. be used.
  120. You may add custom fields if required. Make sure to namespace
  121. them by prepending an underscore and the name of your application:
  122. .Setting a custom field
  123. [source,erlang]
  124. ----
  125. Req#{_myapp_auth_method => pubkey}.
  126. ----
  127. === resp_body()
  128. [source,erlang]
  129. ----
  130. resp_body() :: iodata()
  131. | {sendfile, Offset, Length, Filename}
  132. Offset :: non_neg_integer()
  133. Length :: non_neg_integer()
  134. Filename :: file:name_all()
  135. ----
  136. Response body.
  137. It can take two forms: the actual data to be sent or a
  138. tuple indicating which file to send.
  139. When sending data directly, the type is either a binary or
  140. an iolist. Iolists are an efficient way to build output.
  141. Instead of concatenating strings or binaries, you can simply
  142. build a list containing the fragments you want to send in the
  143. order they should be sent:
  144. .Example iolists usage
  145. [source,erlang]
  146. ----
  147. 1> RespBody = ["Hello ", [<<"world">>, $!]].
  148. ["Hello ",[<<"world">>,33]]
  149. 2> io:format("~s~n", [RespBody]).
  150. Hello world!
  151. ----
  152. Note that the length must be greater than zero for any data
  153. to be sent. Cowboy will send an empty body when the length
  154. is zero.
  155. == See also
  156. link:man:cowboy(7)[cowboy(7)]