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
54 def POST
55   env = @env
56   if post_params = (env["roda.json_params"] || env["rack.request.form_hash"])
57     post_params
58   elsif (input = env["rack.input"]) && content_type =~ /json/
59     str = _read_json_input(input)
60     return super if str.empty?
61     begin
62       json_params = parse_json(str)
63     rescue
64       roda_class.opts[:json_parser_error_handler].call(self)
65     end
66 
67     wrap = roda_class.opts[:json_parser_wrap]
68     if wrap == :always || (wrap == :unless_hash && !json_params.is_a?(Hash))
69       json_params = {"_json"=>json_params}
70     end
71     env["roda.json_params"] = json_params
72     env["rack.request.form_input"] = input
73     json_params
74   else
75     super
76   end
77 end