cowboy_rest.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. = cowboy_rest(3)
  2. == Name
  3. cowboy_rest - REST handlers
  4. == Description
  5. The `cowboy_rest` module implements REST semantics on top of
  6. the HTTP protocol.
  7. This module is a sub protocol that defines many callbacks
  8. be implemented by handlers. The `init/2` and `terminate/3`
  9. callbacks are common to all handler types and are documented
  10. in the manual for the link:cowboy_handler.asciidoc[cowboy_handler] module.
  11. All other callbacks are optional, though some may become
  12. required depending on the return value of previous callbacks.
  13. == Meta values
  14. charset = binary()::
  15. Negotiated charset.
  16. +
  17. This value may not be defined if no charset was negotiated.
  18. language = binary()::
  19. Negotiated language.
  20. +
  21. This value may not be defined if no language was negotiated.
  22. media_type = {binary(), binary(), '*' | [{binary(), binary()}]}::
  23. Negotiated media-type.
  24. +
  25. The media-type is the content-type, excluding the charset.
  26. +
  27. This value is always defined after the call to
  28. `content_types_provided/2`.
  29. == Terminate reasons
  30. The following values may be received as the terminate reason
  31. in the optional `terminate/3` callback.
  32. normal::
  33. The connection was closed normally.
  34. {crash, Class, Reason}::
  35. A crash occurred in the handler. `Class` and `Reason` can be
  36. used to obtain more information about the crash. The function
  37. `erlang:get_stacktrace/0` can also be called to obtain the
  38. stacktrace of the process when the crash occurred.
  39. == Callbacks
  40. === Callback(Req, State) -> {Value, Req, State} | {stop, Req, State}
  41. Callback:: One of the REST callbacks described below.
  42. Req = cowboy_req:req():: The Req object.
  43. State = any():: Handler state.
  44. Value:: See the REST callbacks description below.
  45. Please see the REST callbacks description below for details
  46. on the `Value` type, the default value if the callback is
  47. not defined, and more general information on when the
  48. callback is called and what its intended use is.
  49. The `stop` tuple can be returned to stop REST processing.
  50. It is up to the resource code to send a reply before that,
  51. otherwise a `204 No Content` will be sent.
  52. == REST callbacks description
  53. === allowed_methods
  54. Methods:: all
  55. Value type:: [binary()]
  56. Default value:: `[<<"GET">>, <<"HEAD">>, <<"OPTIONS">>]`
  57. Return the list of allowed methods.
  58. Methods are case sensitive. Standard methods are always uppercase.
  59. === allow_missing_post
  60. Methods:: POST
  61. Value type:: boolean()
  62. Default value:: true
  63. Return whether POST is allowed when the resource doesn't exist.
  64. Returning `true` here means that a new resource will be
  65. created. The URL to the created resource should also be
  66. returned from the `AcceptResource` callback.
  67. === charsets_provided
  68. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  69. Value type:: [binary()]
  70. Default behavior:: Skip to the next step if undefined.
  71. Return the list of charsets the resource provides.
  72. The list must be ordered in order of preference.
  73. If the accept-charset header was not sent, the first charset
  74. in the list will be selected. Otherwise Cowboy will select
  75. the most appropriate charset from the list.
  76. The chosen charset will be set in the `Req` object as the meta
  77. value `charset`.
  78. While charsets are case insensitive, this callback is expected
  79. to return them as lowercase binary.
  80. === content_types_accepted
  81. Methods:: POST, PUT, PATCH
  82. Value type:: [{binary() | {Type, SubType, Params}, AcceptResource}]
  83. Default behavior:: Crash if undefined.
  84. With types:
  85. * Type = SubType = binary()
  86. * Params = '*' | [{binary(), binary()}]
  87. * AcceptResource = atom()
  88. Return the list of content-types the resource accepts.
  89. The list must be ordered in order of preference.
  90. Each content-type can be given either as a binary string or as
  91. a tuple containing the type, subtype and parameters.
  92. Cowboy will select the most appropriate content-type from the list.
  93. If any parameter is acceptable, then the tuple form should be used
  94. with parameters set to `'*'`. If the parameters value is set to `[]`
  95. only content-type values with no parameters will be accepted. All
  96. parameter values are treated in a case sensitive manner except the
  97. `charset` parameter, if present, which is case insensitive.
  98. This function will be called for POST, PUT and PATCH requests.
  99. It is entirely possible to define different callbacks for different
  100. methods if the handling of the request differs. Simply verify
  101. what the method is with `cowboy_req:method/1` and return a
  102. different list for each methods.
  103. The `AcceptResource` value is the name of the callback that will
  104. be called if the content-type matches. It is defined as follows.
  105. Value type:: true | {true, URL} | false
  106. Default behavior:: Crash if undefined.
  107. Process the request body.
  108. This function should create or update the resource with the
  109. information contained in the request body. This information
  110. may be full or partial depending on the request method.
  111. If the request body was processed successfully, `true` must
  112. be returned. If the request method is POST, `{true, URL}` may
  113. be returned instead, and Cowboy will redirect the client to
  114. the location of the newly created resource.
  115. If a response body must be sent, the appropriate media-type, charset
  116. and language can be retrieved using the `cowboy_req:meta/{2,3}`
  117. functions. The respective keys are `media_type`, `charset`
  118. and `language`. The body can be set using `cowboy_req:set_resp_body/2`.
  119. === content_types_provided
  120. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  121. Value type:: [{binary() | {Type, SubType, Params}, ProvideResource}]
  122. Default value:: `[{{<<"text">>, <<"html">>, '*'}, to_html}]`
  123. With types:
  124. * Type = SubType = binary()
  125. * Params = '*' | [{binary(), binary()}]
  126. * ProvideResource = atom()
  127. Return the list of content-types the resource provides.
  128. The list must be ordered in order of preference.
  129. Each content-type can be given either as a binary string or as
  130. a tuple containing the type, subtype and parameters.
  131. Cowboy will select the most appropriate content-type from the list.
  132. If any parameter is acceptable, then the tuple form should be used
  133. with parameters set to `'*'`. If the parameters value is set to `[]`
  134. only content-type values with no parameters will be accepted. All
  135. parameter values are treated in a case sensitive manner except the
  136. `charset` parameter, if present, which is case insensitive.
  137. The `ProvideResource` value is the name of the callback that will
  138. be called if the content-type matches. It will only be called when
  139. a representation of the resource needs to be returned. It is defined
  140. as follow.
  141. Methods:: GET, HEAD
  142. Value type:: iodata() | {stream, Fun} | {stream, Len, Fun} | {chunked, ChunkedFun}
  143. Default behavior:: Crash if undefined.
  144. Return the response body.
  145. The response body may be provided directly or through a fun.
  146. If a fun tuple is returned, the appropriate `set_resp_body_fun`
  147. function will be called. Please refer to the documentation for
  148. these functions for more information about the types.
  149. The call to this callback happens a good time after the call to
  150. `content_types_provided/2`, when it is time to start rendering
  151. the response body.
  152. === delete_completed
  153. Methods:: DELETE
  154. Value type:: boolean()
  155. Default value:: true
  156. Return whether the delete action has been completed.
  157. This function should return `false` if there is no guarantee
  158. that the resource gets deleted immediately from the system,
  159. including from any internal cache.
  160. When this function returns `false`, a `202 Accepted`
  161. response will be sent instead of a `200 OK` or `204 No Content`.
  162. === delete_resource
  163. Methods:: DELETE
  164. Value type:: boolean()
  165. Default value:: false
  166. Delete the resource.
  167. The value returned indicates if the action was successful,
  168. regardless of whether the resource is immediately deleted
  169. from the system.
  170. === expires
  171. Methods:: GET, HEAD
  172. Value type:: calendar:datetime() | binary() | undefined
  173. Default value:: undefined
  174. Return the date of expiration of the resource.
  175. This date will be sent as the value of the expires header.
  176. === forbidden
  177. Methods:: all
  178. Value type:: boolean()
  179. Default value:: false
  180. Return whether access to the resource is forbidden.
  181. A `403 Forbidden` response will be sent if this
  182. function returns `true`. This status code means that
  183. access is forbidden regardless of authentication,
  184. and that the request shouldn't be repeated.
  185. === generate_etag
  186. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  187. Value type:: binary() | {weak | strong, binary()}
  188. Default value:: undefined
  189. Return the entity tag of the resource.
  190. This value will be sent as the value of the etag header.
  191. If a binary is returned, then the value will be parsed
  192. to the tuple form automatically. The value must be in
  193. the same format as the etag header, including quotes.
  194. === is_authorized
  195. Methods:: all
  196. Value type:: true | {false, AuthHeader}
  197. Default value:: true
  198. With types:
  199. * AuthHead = iodata()
  200. Return whether the user is authorized to perform the action.
  201. This function should be used to perform any necessary
  202. authentication of the user before attempting to perform
  203. any action on the resource.
  204. If the authentication fails, the value returned will be sent
  205. as the value for the www-authenticate header in the
  206. `401 Unauthorized` response.
  207. === is_conflict
  208. Methods:: PUT
  209. Value type:: boolean()
  210. Default value:: false
  211. Return whether the put action results in a conflict.
  212. A `409 Conflict` response will be sent if this function
  213. returns `true`.
  214. === known_methods
  215. Methods:: all
  216. Value type:: [binary()]
  217. Default value:: `[<<"GET">>, <<"HEAD">>, <<"POST">>, <<"PUT">>, <<"PATCH">>, <<"DELETE">>, <<"OPTIONS">>]`
  218. Return the list of known methods.
  219. The full list of methods known by the server should be
  220. returned, regardless of their use in the resource.
  221. The default value lists the methods Cowboy knows and
  222. implement in `cowboy_rest`.
  223. Methods are case sensitive. Standard methods are always uppercase.
  224. === languages_provided
  225. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  226. Value type:: [binary()]
  227. Default behavior:: Skip to the next step if undefined.
  228. Return the list of languages the resource provides.
  229. The list must be ordered in order of preference.
  230. If the accept-language header was not sent, the first language
  231. in the list will be selected. Otherwise Cowboy will select
  232. the most appropriate language from the list.
  233. The chosen language will be set in the `Req` object as the meta
  234. value `language`.
  235. While languages are case insensitive, this callback is expected
  236. to return them as lowercase binary.
  237. === last_modified
  238. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  239. Value type:: calendar:datetime()
  240. Default value:: undefined
  241. Return the date of last modification of the resource.
  242. This date will be used to test against the if-modified-since
  243. and if-unmodified-since headers, and sent as the last-modified
  244. header in the response of GET and HEAD requests.
  245. === malformed_request
  246. Methods:: all
  247. Value type:: boolean()
  248. Default value:: false
  249. Return whether the request is malformed.
  250. Cowboy has already performed all the necessary checks
  251. by the time this function is called, so few resources
  252. are expected to implement it.
  253. The check is to be done on the request itself, not on
  254. the request body, which is processed later.
  255. === moved_permanently
  256. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  257. Value type:: {true, URL} | false
  258. Default value:: false
  259. With types:
  260. * URL = iodata()
  261. Return whether the resource was permanently moved.
  262. If it was, its new URL is also returned and sent in the
  263. location header in the response.
  264. === moved_temporarily
  265. Methods:: GET, HEAD, POST, PATCH, DELETE
  266. Value type:: {true, URL} | false
  267. Default value:: false
  268. With types:
  269. * URL = iodata()
  270. Return whether the resource was temporarily moved.
  271. If it was, its new URL is also returned and sent in the
  272. location header in the response.
  273. === multiple_choices
  274. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  275. Value type:: boolean()
  276. Default value:: false
  277. Return whether there are multiple representations of the resource.
  278. This function should be used to inform the client if there
  279. are different representations of the resource, for example
  280. different content-type. If this function returns `true`,
  281. the response body should include information about these
  282. different representations using `cowboy_req:set_resp_body/2`.
  283. The content-type of the response should be the one previously
  284. negociated and that can be obtained by calling
  285. `cowboy_req:meta(media_type, Req)`.
  286. === options
  287. Methods:: OPTIONS
  288. Value type:: ok
  289. Default value:: ok
  290. Handle a request for information.
  291. The response should inform the client the communication
  292. options available for this resource.
  293. By default, Cowboy will send a `200 OK` response with the
  294. allow header set.
  295. === previously_existed
  296. Methods:: GET, HEAD, POST, PATCH, DELETE
  297. Value type:: boolean()
  298. Default value:: false
  299. Return whether the resource existed previously.
  300. === resource_exists
  301. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  302. Value type:: boolean()
  303. Default value:: true
  304. Return whether the resource exists.
  305. If it exists, conditional headers will be tested before
  306. attempting to perform the action. Otherwise, Cowboy will
  307. check if the resource previously existed first.
  308. === service_available
  309. Methods:: all
  310. Value type:: boolean()
  311. Default value:: true
  312. Return whether the service is available.
  313. This function can be used to test that all relevant backend
  314. systems are up and able to handle requests.
  315. A `503 Service Unavailable` response will be sent if this
  316. function returns `false`.
  317. === uri_too_long
  318. Methods:: all
  319. Value type:: boolean()
  320. Default value:: false
  321. Return whether the requested URI is too long.
  322. Cowboy has already performed all the necessary checks
  323. by the time this function is called, so few resources
  324. are expected to implement it.
  325. A `414 Request-URI Too Long` response will be sent if this
  326. function returns `true`.
  327. === valid_content_headers
  328. Methods:: all
  329. Value type:: boolean()
  330. Default value:: true
  331. Return whether the content-* headers are valid.
  332. This also applies to the transfer-encoding header. This
  333. function must return `false` for any unknown content-*
  334. headers, or if the headers can't be understood. The
  335. function `cowboy_req:parse_header/2` can be used to
  336. quickly check the headers can be parsed.
  337. A `501 Not Implemented` response will be sent if this
  338. function returns `false`.
  339. === valid_entity_length
  340. Methods:: all
  341. Value type:: boolean()
  342. Default value:: true
  343. Return whether the request body length is within acceptable boundaries.
  344. A `413 Request Entity Too Large` response will be sent if this
  345. function returns `false`.
  346. === variances
  347. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  348. Value type:: [binary()]
  349. Default value:: []
  350. Return the list of headers that affect the representation of the resource.
  351. These request headers return the same resource but with different
  352. parameters, like another language or a different content-type.
  353. Cowboy will automatically add the accept, accept-language and
  354. accept-charset headers to the list if the respective functions
  355. were defined in the resource.
  356. This operation is performed right before the `resource_exists/2`
  357. callback. All responses past that point will contain the vary
  358. header which holds this list.