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