Skip to content
Documentation
api-platform
connect
middleware
Serializer Middleware

Serializer Middleware

Configuration

The middleware is configured by defining some serializers.


_17
serializersMiddleware(
_17
[
_17
{
_17
regex: /^application\/xml$/,
_17
serializer: (data) => `<message>${data}</message>`,
_17
},
_17
{
_17
regex: /^application\/json$/,
_17
serializer: (data) => JSON.stringify(data)
_17
},
_17
{
_17
regex: /^text\/plain$/,
_17
serializer: (data) => data
_17
}
_17
],
_17
'application/json'
_17
)

The defaultContentType (optional) option is used if the request and handler don't specify what type is wanted.

Serializer Functions

When a matching serializer is found, the Content-Type header is set and the serializer function is run.

The function is passed the entire data object, and should return a string.

If a string is returned, the body attribute of the response is updated.

If an object with a body attribute is returned, the entire response object is replaced. This is useful if you want to manipulate headers or add additional attributes in the Lambda response.

Content Type Negotiation

The header is not the only way the middleware decides which serializer to execute.

The content type is determined in the following order:

All options allow for multiple types to be specified in your order of preference, and the first matching serializer will be executed.