99Essentially, content negotiation is the * client* telling the server what it is sending and what it wants in return, and
1010the server determining if it can do what the client requests.
1111
12- Content negotiation validation in ** DotKernel API** happened through middleware, and it ensures that the incoming
12+ Content negotiation validation in ** DotKernel API** happens through middleware, and it ensures that the incoming
1313request and the outgoing response conform to the content types specified in the config file for all routes or for a
1414specific route.
1515
@@ -18,8 +18,9 @@ errors responses when necessary.
1818
1919## Configuration
2020
21- In DotKernel the configuration file for content negotiation is held on ` config/autoload/content-negotiation.global.php `
22- and the array look like this:
21+ In DotKernel API the configuration file for content negotiation is held
22+ in ` config/autoload/content-negotiation.global.php `
23+ and the array looks like this:
2324
2425``` php
2526return [
@@ -42,18 +43,19 @@ return [
4243];
4344```
4445
45- Except the ` default ` key, all your key must match the route name, for example in DotKernel we have the route to list all
46- admins, which name is ` admin.list ` .
46+ Except the ` default ` key, all your keys must match the route name, for example in DotKernel API we have the route to
47+ list all admins, which name is ` admin.list ` .
4748
48- If you did not specify a route name to configure you specifications about content negotiation, the ` default ` one will
49+ If you did not specify a route name to configure your specifications about content negotiation, the ` default ` one will
4950be in place. The ` default ` key is ` mandatory ` .
5051
5152Every route configuration must come with ` Accept ` and ` Content-Type ` keys, basically this will be the keys that the
5253request headers will be validated against.
5354
5455## Accept Negotiation
5556
56- This specifies that your server can return that representation, or at least one the representation send from the client
57+ This specifies that your server can return that representation, or at least one of the representation sent by the
58+ client.
5759
5860``` shell
5961GET /admin HTTP/1.1
@@ -68,17 +70,16 @@ If the representation cannot be returned, a status code `406 - Not Acceptable` w
6870If the representation can be returned, the server should report the media type through ` Content-Type ` header of the
6971response.
7072
71- > Due to how these validations are make , for a ` json ` media type, the server can return a more generic media type,
73+ > Due to how these validations are made , for a ` json ` media type, the server can return a more generic media type,
7274> for example, if the clients send ` Accept: application/vnd.api+json ` and you configured your ` Accept ` key
73- > as ` application/json `
74- > the representation will be returned as is still json.
75+ > as ` application/json ` the representation will still be returned as ` json ` .
7576
76- > If the ` Accept ` header of the request contains ` */* ` it means that whatever the server can return is OK, so it can
77- > return anything
77+ > If the ` Accept ` header of the request contains ` */* ` it means that whatever the server can return it is OK, so it can
78+ > return anything.
7879
7980## Content-Type Negotiation
8081
81- This aspect of content negotiation is the ` Content-Type ` key and determining if the server can deserialize the data.
82+ The second aspect of content negotiation is the ` Content-Type ` header and determine the server can deserialize the data.
8283
8384``` shell
8485POST /admin/1 HTTP/1.1
@@ -89,21 +90,23 @@ Content-Type: application/json
8990}
9091```
9192
92- The server will try to validate this ` Content-Type ` against your configured ` Content-Type ` key from the config file,
93- and if the format is not supported, a status code ` 415 - Unsupported Media Type ` will be returned.
93+ The server will try to validate the ` Content-Type ` header against your configured ` Content-Type ` key from the config
94+ file, and if the format is not supported, a status code ` 415 - Unsupported Media Type ` will be returned.
9495
95- For example, you have a route that it needs an upload file, normally you will configure the ` Content-Type ` of that route
96- to be ` multipart/form-data ` . The above request will fail as the client send ` application/json ` as ` Content-Type `
96+ For example, if you have a route that needs a file to be uploaded , normally you will configure the ` Content-Type ` of
97+ that route to be ` multipart/form-data ` . The above request will fail as the client send ` application/json ` as
98+ ` Content-Type ` .
9799
98- > If the request does not contain "Content-Type" header, that means that the server will try to deserialize tha data as
99- > he can.
100+ > If the request does not contain "Content-Type" header, that means that the server will try to deserialize the data as
101+ > it can.
100102
101103## The ` Request <-> Response ` validation
102104
103- In addition to the validation described above, a third one is happening and is the last one, the server will check if
105+ In addition to the validation described above, a third one is happening and is the last one: the server will check if
104106the request ` Accept ` header can really be returned by the response.
105107
106- Through how the ** DotKernel API** is returning a response in handler , a content type is always set, but this cannot be
107- the case in any custom response but in any way the server will check what ` Content-Type ` the response is returning and
108- will try to validate that against the ` Accept ` header of the request. If the validation fails, a status code
109- ` 406 - Not Acceptable ` will be returned.
108+ Through the way ** DotKernel API** is returning a response in handler, a content type is always set.
109+
110+ This cannot be the case in any custom response but in any case the server will check what ` Content-Type ` the response is
111+ returning and will try to validate that against the ` Accept ` header of the request.
112+ If the validation fails, a status code ` 406 - Not Acceptable ` will be returned.
0 commit comments