class
Amber::Schema::Parser::ParserRegistry
- Amber::Schema::Parser::ParserRegistry
- Reference
- Object
Overview
Registry for content-type based parser selection
Defined in:
amber/schema/parser.crClass Method Summary
-
.get(content_type : String | Nil) : String | Nil
Get parser name for content type
-
.parse_request(request : HTTP::Request, schema_class : Definition.class | Nil = nil) : Hash(String, JSON::Any)
Parse request based on content type
- .parsers
- .parsers=(parsers : Hash(String, String))
-
.register(content_type : String, parser_name : String)
Register a parser for a content type
-
.register_body_parser(content_type : String, &parser : IO -> Hash(String, JSON::Any)) : Nil
Opts an application into parsing a request media type that Amber does not parse by default, such as XML.
- .request_body_parsers
-
.xml_media_type?(media_type : String) : Bool
Amber V2 parses request bodies as JSON and form data.
Class Method Detail
Get parser name for content type
Parse request based on content type
Register a parser for a content type
Opts an application into parsing a request media type that Amber does
not parse by default, such as XML. Call it once at boot, for example
in config/initializers/request_body_parsers.cr:
Amber::Schema::Parser::ParserRegistry.register_body_parser("application/xml") do |body|
MyXMLReader.read_fields(body) # => Hash(String, JSON::Any)
end
The schema for the action must also list the media type in its
content_type declaration. A parser that raises answers the request
with 400 invalid_request_body. Media types without a registered
parser keep their default answer; XML types are answered with 415.
Amber V2 parses request bodies as JSON and form data. By default XML
bodies (application/xml, text/xml, and any +xml suffix type)
are answered with 415 Unsupported Media Type, so no XML parser is
linked into applications. An application opts in to XML with
.register_body_parser.