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
73 def POST
74   env = @env
75   if post_params = env["roda.json_params"]
76     return post_params
77   end
78 
79   unless (input = env["rack.input"]) && (content_type = self.content_type) && roda_class.opts[:json_parser_content_type_regexp].send(MATCH_METHOD, content_type)
80     return super
81   end
82 
83   str = _read_json_input(input)
84   return super if str.empty?
85   begin
86     json_params = parse_json(str)
87   rescue
88     roda_class.opts[:json_parser_error_handler].call(self)
89   end
90 
91   wrap = roda_class.opts[:json_parser_wrap]
92   if wrap == :always || (wrap == :unless_hash && !json_params.is_a?(Hash))
93     json_params = {"_json"=>json_params}
94   end
95   env["roda.json_params"] = json_params
96   json_params
97 end