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