Added JSON body parsing

This commit is contained in:
Sdogruyol 2015-11-06 20:24:38 +02:00
parent 5e1460bd31
commit 3687897005
3 changed files with 93 additions and 1 deletions

View file

@ -1,13 +1,19 @@
require "json"
# ParamParser parses the request contents including query_params and body
# and converts them into a params hash which you can within the environment
# context.
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)
class Kemal::ParamParser
URL_ENCODED_FORM = "application/x-www-form-urlencoded"
APPLICATION_JSON = "application/json"
def initialize(@route, @request)
@route_components = route.components
@request_components = request.path.not_nil!.split "/"
@params = {} of String => String
@params = {} of String => AllParamTypes
end
def parse
@ -18,6 +24,7 @@ class Kemal::ParamParser
def parse_request
parse_query
parse_body
parse_json
@params
end
@ -30,6 +37,15 @@ class Kemal::ParamParser
parse_part(@request.query)
end
def parse_json
return unless @request.headers["Content-Type"]? == APPLICATION_JSON
body = @request.body as String
json = JSON.parse(body) as Hash
json.each do |k, v|
@params[k as String] = v as AllParamTypes
end
end
def parse_part(part)
return unless part
HTTP::Params.parse(part) do |key, value|