You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The API system has built-in [content negotiation](https://en.wikipedia.org/wiki/Content_negotiation) capabilities.
4
-
It leverages the [`willdurand/negotiation`](https://github.com/willdurand/Negotiation) library.
5
4
6
5
By default, only the [JSON-LD](https://json-ld.org) format is enabled. However API Platform Core supports many more formats and can be extended.
7
6
8
-
The framework natively supports JSON-LD, GraphQL, JSONAPI, HAL, raw JSON, XML, YAML and CSV (YAML and CSV support is only available if you use Symfony 3.2+).
7
+
The framework natively supports JSON-LD (and Hydra), GraphQL, JSON:API, HAL, YAML, CSV, HTML (API docs), raw JSON and raw XML.
8
+
Using the raw JSON or raw XML formats is discouraged, prefer using JSON-LD instead, which provides more feature and is as easy to use.
9
9
10
-
Both XML and JSON formats are experimental and there is no assurance that we will not break them.
10
+
API Platform also supports [JSON Merge Patch (RFC 7396)](https://tools.ietf.org/html/rfc7396) the JSON:API[`PATCH`](https://tools.ietf.org/html/rfc5789) formats, as well as [Problem Details (RFC 7807)](https://tools.ietf.org/html/rfc7807), Hydra and JSON:API error formats.
11
11
12
12
API Platform Core will automatically detect the best resolving format depending on:
13
13
14
14
* enabled formats (see below)
15
-
* the requested format, specified in either the `Accept` HTTP header or as an extension appended to the URL
15
+
* the requested format, specified in either [the `Accept` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) or as an extension appended to the URL
16
16
17
17
Available formats are:
18
18
19
19
Format | Format name | MIME types | Backward Compatibility guaranteed
[JSON](https://www.json.org/) | `json` | `application/json` | no
26
-
[XML](https://www.w3.org/XML/) | `xml` | `application/xml`, `text/xml` | no
27
25
[YAML](http://yaml.org/) | `yaml` | `application/x-yaml` | no
28
26
[CSV](https://tools.ietf.org/html/rfc4180) | `csv` | `text/csv` | no
29
27
[HTML](https://whatwg.org/) (API docs) | `html` | `text/html` | no
28
+
[XML](https://www.w3.org/XML/) | `xml` | `application/xml`, `text/xml` | no
29
+
[JSON](https://www.json.org/) | `json` | `application/json` | no
30
30
31
-
If the client's requested format is not specified (if it's not supported, it will throw an HTTP bad request error), the response format will be the first format defined in the `formats` configuration key (see below).
32
-
An example using the built-in XML support is available in [Behat specs](https://github.com/api-platform/core/blob/master/features/main/content_negotiation.feature).
31
+
If the client's requested format is not specified, the response format will be the first format defined in the `formats` configuration key (see below).
32
+
If the request format is not supported, an [Unsupported Media Type](https://developer.mozilla.org/fr/docs/Web/HTTP/Status/415) error will be returned.
33
33
34
-
The API Platform content negotiation system is extendable. Support for other formats can be added by [creating and registering appropriate encoders and, sometimes, normalizers](https://symfony.com/doc/current/serializer.html#adding-normalizers-and-encoders). Adding support for other
35
-
standard hypermedia formats upstream is welcome. Don't hesitate to contribute by adding your encoders and normalizers
36
-
to API Platform Core.
34
+
Examples showcasing how to use the different mechanisms are available [in the API Platform test suite](https://github.com/api-platform/core/blob/master/features/main/content_negotiation.feature).
37
35
38
-
## Enabling Several Formats
36
+
## Configuring Formats Globally
39
37
40
38
The first required step is to configure allowed formats. The following configuration will enable the support of XML (built-in)
41
39
and of a custom format called `myformat` and having `application/vnd.myformat` as [MIME type](https://en.wikipedia.org/wiki/Media_type).
42
40
43
41
```yaml
44
42
# api/config/packages/api_platform.yaml
45
43
api_platform:
46
-
# ...
47
-
48
44
formats:
49
45
jsonld: ['application/ld+json']
50
46
jsonhal: ['application/hal+json']
@@ -54,7 +50,6 @@ api_platform:
54
50
yaml: ['application/x-yaml']
55
51
csv: ['text/csv']
56
52
html: ['text/html']
57
-
myformat: ['application/vnd.myformat']
58
53
```
59
54
60
55
To enable GraphQL support, [read the dedicated chapter](graphql.md).
@@ -63,10 +58,43 @@ Because the Symfony Serializer component is able to serialize objects in XML, se
63
58
`text/xml`string as value is enough to retrieve XML documents from our API. However API Platform knows nothing about the
64
59
`myformat`format. We need to register an encoder and optionally a normalizer for this format.
65
60
61
+
## Configuring PATCH Formats
62
+
63
+
By default, API Platform supports JSON Merge Patch and JSON:API PATCH formats.
64
+
Support for the JSON:API PATCH format is automatically enabled if JSON:API support is enabled.
65
+
JSON Merge Patch support must be enabled explicitly:
66
+
67
+
```yaml
68
+
# api/config/packages/api_platform.yaml
69
+
api_platform:
70
+
patch_formats:
71
+
json: ['application/merge-patch+json']
72
+
jsonapi: ['application/vnd.api+json']
73
+
```
74
+
75
+
When support for at least one PATCH format is enabled, [an `Accept-Patch` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Patch) containing the list of supported patch formats is automatically added to all HTTP responses for items.
76
+
77
+
## Configuring Error Formats
66
78
67
-
## Enabling Additional Formats On a Specific Resource/Operation
79
+
API Platform will try to send to the client the error format matching with the format request with the `Accept` HTTP headers (or the URL extension). For instance, if a client request a JSON-LD representation of a resource, and an error occurs, then API Platform will serialize this error using the Hydra format (Hydra is a vocabulary for JSON-LD containing a standard representation of API errors).
68
80
69
-
Support for specific formats can also be added at resource or operation level, using the `formats` attribute.
@@ -89,7 +117,8 @@ Additionally the `csv` format is added with the MIME type `text/csv`.
89
117
It is also important to notice that the usage of this attribute will override the formats defined in the configuration, therefore
90
118
this configuration might disable the `json` or the `html` on this resource for example.
91
119
92
-
You can specify different accepted formats at operation level too:
120
+
You can specify different accepted formats at operation level too, it's especially convenient for to configure formats available for the `PATH` method:
<!-- works also with <attribute name="json">application/merge-patch+json</attribute> -->
180
+
</attribute>
181
+
</itemOperation>
182
+
</itemOperations>
135
183
</resource>
136
184
</resources>
137
185
```
138
-
YAML:
139
-
```yaml
140
-
resources:
141
-
App\Entity\Book:
142
-
collectionOperations:
143
-
get:
144
-
formats:
145
-
xml: ['text/xml'] # works also with "text/html"
146
-
attributes:
147
-
formats:
148
-
0: 'jsonld' # format already defined in the config
149
-
csv: 'text/csv'
150
-
```
151
186
152
-
## Registering a Custom Serializer
187
+
## Supporting Custom Formats
188
+
189
+
The API Platform content negotiation system is extendable.
190
+
You can add support for formats not available by default by creating custom normalizer and encoders.
191
+
Refer to the Symfony documentation to learn [how to create and register such classes](https://symfony.com/doc/current/serializer.html#adding-normalizers-and-encoders).
192
+
193
+
Then, register the new format in the configuration:
153
194
154
-
If you are adding support for a format supported by default neither by API Platform nor by the Symfony Serializer Component,
155
-
you need to create a custom encoder, decoder and eventually a normalizer and a denormalizer. Refer to the
156
-
Symfony documentation to learn [how to create and register such classes](https://symfony.com/doc/current/cookbook/serializer.html#adding-normalizers-and-encoders).
195
+
```yaml
196
+
# api/config/packages/api_platform.yaml
197
+
api_platform:
198
+
formats:
199
+
# ...
200
+
myformat: ['application/vnd.myformat']
201
+
```
157
202
158
-
API Platform Core will automatically call the serializer with your defined format name (`myformat` in previous examples)
159
-
as `format` parameter during the deserialization process. It will then return the result to the client with the requested MIME
160
-
type using its built-in responder.
203
+
API Platform Core will automatically call the serializer with your defined format name as `format` parameter during the deserialization process (`myformat` in the example).
204
+
It will then return the result to the client with the requested MIME type using its built-in responder.
205
+
For non-standard formats, [a vendor, vanity or unregistered MIME type should be used](https://en.wikipedia.org/wiki/Media_type#Vendor_tree).
161
206
162
-
## Writing a Custom Normalizer
207
+
### Reusing the API Platform Infrastructure
163
208
164
209
Using composition is the recommended way to implement a custom normalizer. You can use the following template to start your
165
210
own implementation of `CustomItemNormalizer`:
@@ -255,3 +300,8 @@ class CustomItemNormalizer implements NormalizerInterface, DenormalizerInterface
255
300
// ...
256
301
}
257
302
```
303
+
304
+
### Contributing Support for New Formats
305
+
306
+
Adding support for **standard** formats upstream is welcome!
307
+
We'll be glad to merge new encoders and normalizers in API Platform Core.
0 commit comments