Module: Roda::RodaPlugins::JsonParser::RequestMethods

Defined in:
lib/roda/plugins/json_parser.rb

Instance Method Summary collapse

Instance Method Details

#POSTObject

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/roda/plugins/json_parser.rb', line 54

def POST
  env = @env
  if post_params = (env["roda.json_params"] || env["rack.request.form_hash"])
    post_params
  elsif (input = env["rack.input"]) && content_type =~ /json/
    str = _read_json_input(input)
    return super if str.empty?
    begin
      json_params = parse_json(str)
    rescue
      roda_class.opts[:json_parser_error_handler].call(self)
    end

    wrap = roda_class.opts[:json_parser_wrap]
    if wrap == :always || (wrap == :unless_hash && !json_params.is_a?(Hash))
      json_params = {"_json"=>json_params}
    end
    env["roda.json_params"] = json_params
    env["rack.request.form_input"] = input
    json_params
  else
    super
  end
end