diff options
Diffstat (limited to 'guide')
-rw-r--r-- | guide/rest_handlers.md | 13 | ||||
-rw-r--r-- | guide/static_handlers.md | 32 | ||||
-rw-r--r-- | guide/toc.md | 1 |
3 files changed, 44 insertions, 2 deletions
diff --git a/guide/rest_handlers.md b/guide/rest_handlers.md index d5997f9..ac11d98 100644 --- a/guide/rest_handlers.md +++ b/guide/rest_handlers.md @@ -106,6 +106,19 @@ each function. For example, `from_html` and `to_html` indicate in the first case that we're accepting a resource given as HTML, and in the second case that we send one as HTML. +Cowboy will set informative meta values at various points of the +execution. You can retrieve them using `cowboy_req:meta/{2,3}`. +The values are defined in the following table. + +| Meta key | Details | +| -----------| ---------------------------------------------------- | +| media_type | The content-type negotiated for the response entity. | +| language | The language negotiated for the response entity. | +| charset | The charset negotiated for the response entity. | + +They can be used to reply a response entity to a request with +an idempotent method (`POST`, `PUT`, `PATCH`, `DELETE`). + Usage ----- diff --git a/guide/static_handlers.md b/guide/static_handlers.md index 0843599..d6347f0 100644 --- a/guide/static_handlers.md +++ b/guide/static_handlers.md @@ -19,8 +19,7 @@ Static handlers are pre-written REST handlers. They only need to be specified in the routing information with the proper options. The following example routing serves all files found in the -`priv_dir/static/` directory of the application `my_app`. It uses -a mimetypes library to figure out the files' content types. +`priv_dir/static/` directory of the application `my_app`. ``` erlang Dispatch = [ @@ -32,3 +31,32 @@ Dispatch = [ ]} ]. ``` + +You can also serve a single file specifically. A common example +would be an `index.html` file to be served when the path `/` +is requested. The following example will serve the `priv/index.html` +file from the application `my_app`. + +``` erlang +Dispatch = [ + {'_', [ + {"/", cowboy_static, [ + {directory, {priv_dir, my_app, []}}, + {file, "index.html"}, + {mimetypes, {fun mimetypes:path_to_mimes/2, default}} + ]} + ]} +]. +``` + +MIME type +--------- + +Cowboy does not provide any default for MIME types. This means +that unless you specify the `mimetypes` option, all files will +be sent as `application/octet-stream`, which the browser will +not try to interpret, instead trying to make you download it. + +In the examples above we used the +[mimetypes application](https://github.com/spawngrid/mimetypes) +to find the MIME type from the file's extension. diff --git a/guide/toc.md b/guide/toc.md index 6909bed..44c8e22 100644 --- a/guide/toc.md +++ b/guide/toc.md @@ -34,6 +34,7 @@ Cowboy User Guide * [Static handlers](static_handlers.md) * Purpose * Usage + * MIME type * [Request object](req.md) * Purpose * Request |