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

@ -40,4 +40,66 @@ describe "Kemal::Handler" do
response = kemal.call(request) response = kemal.call(request)
response.body.should eq("hello world") response.body.should eq("hello world")
end end
it "parses simple JSON body" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
name = env.params["name"]
age = env.params["age"]
"Hello #{name} Age #{age}"
end
json_payload = {"name": "Serdar", "age": 26}
request = HTTP::Request.new(
"POST",
"/",
body: json_payload.to_json,
headers: HTTP::Headers{"Content-Type": "application/json"},
)
response = kemal.call(request)
response.body.should eq("Hello Serdar Age 26")
end
it "parses JSON with string array" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
skills = env.params["skills"] as Array
"Skills #{skills.each.join(',')}"
end
json_payload = {"skills": ["ruby", "crystal"]}
request = HTTP::Request.new(
"POST",
"/",
body: json_payload.to_json,
headers: HTTP::Headers{"Content-Type": "application/json"},
)
response = kemal.call(request)
response.body.should eq("Skills ruby,crystal")
end
it "parses JSON with json object array" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
skills = env.params["skills"] as Array
skills_from_languages = skills.map do |skill|
skill = skill as Hash
skill["language"]
end
"Skills #{skills_from_languages.each.join(',')}"
end
json_payload = {"skills": [{"language": "ruby"}, {"language": "crystal"}]}
request = HTTP::Request.new(
"POST",
"/",
body: json_payload.to_json,
headers: HTTP::Headers{"Content-Type": "application/json"},
)
response = kemal.call(request)
response.body.should eq("Skills ruby,crystal")
end
end end

View file

@ -30,6 +30,20 @@ describe "ParamParser" do
params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"}) params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"})
end end
it "parses request body" do
route = Route.new "POST", "/" { }
request = HTTP::Request.new(
"POST",
"/",
body: "{\"name\": \"Serdar\"}",
headers: HTTP::Headers{"Content-Type": "application/json"},
)
params = Kemal::ParamParser.new(route, request).parse
params.should eq({"name": "Serdar"})
end
context "when content type is incorrect" do context "when content type is incorrect" do
it "does not parse request body" do it "does not parse request body" do
route = Route.new "POST", "/" do |env| route = Route.new "POST", "/" do |env|

View file

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