class Amber::Schema::Parser::ParserRegistry

Overview

Registry for content-type based parser selection

Defined in:

amber/schema/parser.cr

Class Method Summary

Class Method Detail

def self.get(content_type : String | Nil) : String | Nil #

Get parser name for content type


[View source]
def self.parse_request(request : HTTP::Request, schema_class : Definition.class | Nil = nil) : Hash(String, JSON::Any) #

Parse request based on content type. The whole parse runs through HTTP::Request#guard_body_read, so a rejected body stays rejected for every later read of that request.


[View source]
def self.parsers #

[View source]
def self.parsers=(parsers : Hash(String, String)) #

[View source]
def self.register(content_type : String, parser_name : String) #

Register a parser for a content type


[View source]
def self.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. 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. The parser reads the body through a stream capped at params.max_document_body_bytes, and Amber drains whatever it leaves unread through the same cap; a longer body is answered with 400 parameter_limit_exceeded. A parser that raises Amber::Schema::RequestParseError is answered with that error as raised. Any other exception is answered with 400 invalid_request_body, and the response includes the exception's message, so raise messages a client may read (a parse position, not a file path or a secret). Media types without a registered parser keep their default answer; XML types are answered with 415.


[View source]
def self.request_body_parsers #

[View source]
def self.xml_media_type?(media_type : String) : Bool #

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.


[View source]