cowboy_rest.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. // @todo Space required for the time being: https://github.com/spf13/hugo/issues/2398
  123. Default value:: `[{{ <<"text">>, <<"html">>, '*'}, to_html}]`
  124. With types:
  125. * Type = SubType = binary()
  126. * Params = '*' | [{binary(), binary()}]
  127. * ProvideResource = atom()
  128. Return the list of content-types the resource provides.
  129. The list must be ordered in order of preference.
  130. Each content-type can be given either as a binary string or as
  131. a tuple containing the type, subtype and parameters.
  132. Cowboy will select the most appropriate content-type from the list.
  133. If any parameter is acceptable, then the tuple form should be used
  134. with parameters set to `'*'`. If the parameters value is set to `[]`
  135. only content-type values with no parameters will be accepted. All
  136. parameter values are treated in a case sensitive manner except the
  137. `charset` parameter, if present, which is case insensitive.
  138. The `ProvideResource` value is the name of the callback that will
  139. be called if the content-type matches. It will only be called when
  140. a representation of the resource needs to be returned. It is defined
  141. as follow.
  142. Methods:: GET, HEAD
  143. Value type:: iodata() | {stream, Fun} | {stream, Len, Fun} | {chunked, ChunkedFun}
  144. Default behavior:: Crash if undefined.
  145. Return the response body.
  146. The response body may be provided directly or through a fun.
  147. If a fun tuple is returned, the appropriate `set_resp_body_fun`
  148. function will be called. Please refer to the documentation for
  149. these functions for more information about the types.
  150. The call to this callback happens a good time after the call to
  151. `content_types_provided/2`, when it is time to start rendering
  152. the response body.
  153. === delete_completed
  154. Methods:: DELETE
  155. Value type:: boolean()
  156. Default value:: true
  157. Return whether the delete action has been completed.
  158. This function should return `false` if there is no guarantee
  159. that the resource gets deleted immediately from the system,
  160. including from any internal cache.
  161. When this function returns `false`, a `202 Accepted`
  162. response will be sent instead of a `200 OK` or `204 No Content`.
  163. === delete_resource
  164. Methods:: DELETE
  165. Value type:: boolean()
  166. Default value:: false
  167. Delete the resource.
  168. The value returned indicates if the action was successful,
  169. regardless of whether the resource is immediately deleted
  170. from the system.
  171. === expires
  172. Methods:: GET, HEAD
  173. Value type:: calendar:datetime() | binary() | undefined
  174. Default value:: undefined
  175. Return the date of expiration of the resource.
  176. This date will be sent as the value of the expires header.
  177. === forbidden
  178. Methods:: all
  179. Value type:: boolean()
  180. Default value:: false
  181. Return whether access to the resource is forbidden.
  182. A `403 Forbidden` response will be sent if this
  183. function returns `true`. This status code means that
  184. access is forbidden regardless of authentication,
  185. and that the request shouldn't be repeated.
  186. === generate_etag
  187. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  188. Value type:: binary() | {weak | strong, binary()}
  189. Default value:: undefined
  190. Return the entity tag of the resource.
  191. This value will be sent as the value of the etag header.
  192. If a binary is returned, then the value will be parsed
  193. to the tuple form automatically. The value must be in
  194. the same format as the etag header, including quotes.
  195. === is_authorized
  196. Methods:: all
  197. Value type:: true | {false, AuthHeader}
  198. Default value:: true
  199. With types:
  200. * AuthHead = iodata()
  201. Return whether the user is authorized to perform the action.
  202. This function should be used to perform any necessary
  203. authentication of the user before attempting to perform
  204. any action on the resource.
  205. If the authentication fails, the value returned will be sent
  206. as the value for the www-authenticate header in the
  207. `401 Unauthorized` response.
  208. === is_conflict
  209. Methods:: PUT
  210. Value type:: boolean()
  211. Default value:: false
  212. Return whether the put action results in a conflict.
  213. A `409 Conflict` response will be sent if this function
  214. returns `true`.
  215. === known_methods
  216. Methods:: all
  217. Value type:: [binary()]
  218. Default value:: `[<<"GET">>, <<"HEAD">>, <<"POST">>, <<"PUT">>, <<"PATCH">>, <<"DELETE">>, <<"OPTIONS">>]`
  219. Return the list of known methods.
  220. The full list of methods known by the server should be
  221. returned, regardless of their use in the resource.
  222. The default value lists the methods Cowboy knows and
  223. implement in `cowboy_rest`.
  224. Methods are case sensitive. Standard methods are always uppercase.
  225. === languages_provided
  226. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  227. Value type:: [binary()]
  228. Default behavior:: Skip to the next step if undefined.
  229. Return the list of languages the resource provides.
  230. The list must be ordered in order of preference.
  231. If the accept-language header was not sent, the first language
  232. in the list will be selected. Otherwise Cowboy will select
  233. the most appropriate language from the list.
  234. The chosen language will be set in the `Req` object as the meta
  235. value `language`.
  236. While languages are case insensitive, this callback is expected
  237. to return them as lowercase binary.
  238. === last_modified
  239. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  240. Value type:: calendar:datetime()
  241. Default value:: undefined
  242. Return the date of last modification of the resource.
  243. This date will be used to test against the if-modified-since
  244. and if-unmodified-since headers, and sent as the last-modified
  245. header in the response of GET and HEAD requests.
  246. === malformed_request
  247. Methods:: all
  248. Value type:: boolean()
  249. Default value:: false
  250. Return whether the request is malformed.
  251. Cowboy has already performed all the necessary checks
  252. by the time this function is called, so few resources
  253. are expected to implement it.
  254. The check is to be done on the request itself, not on
  255. the request body, which is processed later.
  256. === moved_permanently
  257. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  258. Value type:: {true, URL} | false
  259. Default value:: false
  260. With types:
  261. * URL = iodata()
  262. Return whether the resource was permanently moved.
  263. If it was, its new URL is also returned and sent in the
  264. location header in the response.
  265. === moved_temporarily
  266. Methods:: GET, HEAD, POST, PATCH, DELETE
  267. Value type:: {true, URL} | false
  268. Default value:: false
  269. With types:
  270. * URL = iodata()
  271. Return whether the resource was temporarily moved.
  272. If it was, its new URL is also returned and sent in the
  273. location header in the response.
  274. === multiple_choices
  275. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  276. Value type:: boolean()
  277. Default value:: false
  278. Return whether there are multiple representations of the resource.
  279. This function should be used to inform the client if there
  280. are different representations of the resource, for example
  281. different content-type. If this function returns `true`,
  282. the response body should include information about these
  283. different representations using `cowboy_req:set_resp_body/2`.
  284. The content-type of the response should be the one previously
  285. negociated and that can be obtained by calling
  286. `cowboy_req:meta(media_type, Req)`.
  287. === options
  288. Methods:: OPTIONS
  289. Value type:: ok
  290. Default value:: ok
  291. Handle a request for information.
  292. The response should inform the client the communication
  293. options available for this resource.
  294. By default, Cowboy will send a `200 OK` response with the
  295. allow header set.
  296. === previously_existed
  297. Methods:: GET, HEAD, POST, PATCH, DELETE
  298. Value type:: boolean()
  299. Default value:: false
  300. Return whether the resource existed previously.
  301. === resource_exists
  302. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  303. Value type:: boolean()
  304. Default value:: true
  305. Return whether the resource exists.
  306. If it exists, conditional headers will be tested before
  307. attempting to perform the action. Otherwise, Cowboy will
  308. check if the resource previously existed first.
  309. === service_available
  310. Methods:: all
  311. Value type:: boolean()
  312. Default value:: true
  313. Return whether the service is available.
  314. This function can be used to test that all relevant backend
  315. systems are up and able to handle requests.
  316. A `503 Service Unavailable` response will be sent if this
  317. function returns `false`.
  318. === uri_too_long
  319. Methods:: all
  320. Value type:: boolean()
  321. Default value:: false
  322. Return whether the requested URI is too long.
  323. Cowboy has already performed all the necessary checks
  324. by the time this function is called, so few resources
  325. are expected to implement it.
  326. A `414 Request-URI Too Long` response will be sent if this
  327. function returns `true`.
  328. === valid_content_headers
  329. Methods:: all
  330. Value type:: boolean()
  331. Default value:: true
  332. Return whether the content-* headers are valid.
  333. This also applies to the transfer-encoding header. This
  334. function must return `false` for any unknown content-*
  335. headers, or if the headers can't be understood. The
  336. function `cowboy_req:parse_header/2` can be used to
  337. quickly check the headers can be parsed.
  338. A `501 Not Implemented` response will be sent if this
  339. function returns `false`.
  340. === valid_entity_length
  341. Methods:: all
  342. Value type:: boolean()
  343. Default value:: true
  344. Return whether the request body length is within acceptable boundaries.
  345. A `413 Request Entity Too Large` response will be sent if this
  346. function returns `false`.
  347. === variances
  348. Methods:: GET, HEAD, POST, PUT, PATCH, DELETE
  349. Value type:: [binary()]
  350. Default value:: []
  351. Return the list of headers that affect the representation of the resource.
  352. These request headers return the same resource but with different
  353. parameters, like another language or a different content-type.
  354. Cowboy will automatically add the accept, accept-language and
  355. accept-charset headers to the list if the respective functions
  356. were defined in the resource.
  357. This operation is performed right before the `resource_exists/2`
  358. callback. All responses past that point will contain the vary
  359. header which holds this list.