From efe75196f76df690465136fa25dcbbee653158ff Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Mon, 26 Oct 2015 20:22:31 +0200 Subject: [PATCH] Added request body parsing --- spec/kemal_handler_spec.cr | 13 +++++++++++++ src/kemal/handler.cr | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/spec/kemal_handler_spec.cr b/spec/kemal_handler_spec.cr index 37139ef..6ed8b33 100644 --- a/spec/kemal_handler_spec.cr +++ b/spec/kemal_handler_spec.cr @@ -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 diff --git a/src/kemal/handler.cr b/src/kemal/handler.cr index 89e46e1..9adce1d 100644 --- a/src/kemal/handler.cr +++ b/src/kemal/handler.cr @@ -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