Skip to content

Commit

Permalink
Added request body parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sdogruyol committed Oct 26, 2015
1 parent 8a0f9df commit efe7519
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions spec/kemal_handler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions src/kemal/handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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|

This comment has been minimized.

Copy link
@waterlink

waterlink Nov 4, 2015

Contributor

Wouldn't it be better to use body local variable here, since it is already defined and compiler knows it is not nil?

This comment has been minimized.

Copy link
@sdogruyol

sdogruyol Nov 4, 2015

Member

Yeah 👍

params[key] ||= value
end
end

This comment has been minimized.

Copy link
@waterlink

waterlink Nov 4, 2015

Contributor

Should this block of code execute even if Content-Type is not application/x-www-form-urlencoded?

This comment has been minimized.

Copy link
@sdogruyol

sdogruyol Nov 4, 2015

Member

No it should not.


kemal_request = Request.new(request, params)
context = Context.new(kemal_request)
begin
Expand Down

2 comments on commit efe7519

@sdogruyol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like to open a PR for these @waterlink

@waterlink
Copy link
Contributor

@waterlink waterlink commented on efe7519 Nov 5, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.