Added request body parsing

This commit is contained in:
Sdogruyol 2015-10-26 20:22:31 +02:00
parent 8a0f9dffff
commit efe75196f7
2 changed files with 19 additions and 0 deletions

View file

@ -40,4 +40,17 @@ describe "Kemal::Handler" do
response = kemal.call(request)
response.headers["Content-Type"].should eq("application/json")
end
it "parses POST body" do
kemal = Kemal::Handler.new
kemal.add_route "POST", "/" do |env|
name = env.request.params["name"]
age = env.request.params["age"]
hasan = env.request.params["hasan"]
"Hello #{name} #{hasan} #{age}"
end
request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=kemal&age=99")
response = kemal.call(request)
response.body.should eq("Hello kemal cemal 99")
end
end

View file

@ -29,6 +29,12 @@ class Kemal::Handler < HTTP::Handler
params[key] ||= value
end
if body = request.body
HTTP::Params.parse(request.body.not_nil!) do |key, value|
params[key] ||= value
end
end
kemal_request = Request.new(request, params)
context = Context.new(kemal_request)
begin