NAME

Raisin::Middleware::Formatter - A parser/formatter middleware for Raisin.

VERSION

version 0.94

DESCRIPTION

Parses and formats the data it gets from requests and responses if it's needed.

METHODS

call

Invokes an application route. Before doing so, it decodes the request content, if any, and negotiates an output format based on the Accept header of the request and the produces list for the target route. It post-processes the response from the endpoint using the appropriate Decoder, ensuring that it will be a valid PSGI response.

call supports deferred responses by passing the body through unmodified if it's a file handle -- i.e., the result of calling open(), an IO::Handle object, or any blessed reference that supports both getline() and close() methods.

negotiate_format

Negotiates a format from path extension, Accept header or using default format.

A precedence is following:

  • extension; =item * Accept header; =item * default;

In other words if an extension exists the framework doesn't look for Accept header. If the extension is not supported the framework throws an error, the same is for Accept header. Only if both extension and Accept header are not specified does it fallback to default format.

Having picked a format, the correct encoder is set in env->{'rasinx.encoder'}. Occasionally the application will want to change the format, for example to return a JSON error response from a route that normally returns plain text. To do that, change env->{'rasinx.encoder'} to the correct encoder, and make sure that the response Content-Type header matches it. It's the application's responsibility in that case to know what it's doing, and not return content that the client can't accept.

format_from_extension

Extracts an extension from a path, and if exists looks for a formatter.

format_from_header

Parses Accept header for known formatters.

AUTHOR

Artur Khabibullin

COPYRIGHT AND LICENSE

This software is copyright (c) 2019 by Artur Khabibullin.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.