cowboy_rest.ezdoc 14 KB

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