module Roda::RodaPlugins::JsonParser::RequestMethods

  1. lib/roda/plugins/json_parser.rb

Methods

Public Instance

  1. POST

Public Instance methods

POST()

If the Content-Type header in the request includes “json”, parse the request body as JSON. Ignore an empty request body.

[show source]
    # File lib/roda/plugins/json_parser.rb
 83 def POST
 84   env = @env
 85   if post_params = env["roda.json_params"]
 86     return post_params
 87   end
 88 
 89   unless (input = env["rack.input"]) && (content_type = self.content_type) && roda_class.json_parser_content_type_regexp.send(MATCH_METHOD, content_type)
 90     return super
 91   end
 92 
 93   str = _read_json_input(input)
 94   return super if str.empty?
 95   begin
 96     json_params = parse_json(str)
 97   rescue
 98     roda_class.json_parser_error_handler.call(self)
 99   end
100 
101   wrap = roda_class.json_parser_wrap
102   if wrap == :always || (wrap == :unless_hash && !json_params.is_a?(Hash))
103     json_params = {"_json"=>json_params}
104   end
105   env["roda.json_params"] = json_params
106   json_params
107 end