Fix parsing JSON params when "charset" is present in "Content-Type" header (#193)
Resolves case when `Content-Type` headers is passed in form of `application/json; charset=utf-8`
This commit is contained in:
parent
d4e96c7b44
commit
28859fbdb8
2 changed files with 15 additions and 1 deletions
|
@ -91,6 +91,20 @@ describe "ParamParser" do
|
|||
json_params.should eq({"name" => "Serdar"})
|
||||
end
|
||||
|
||||
it "parses request body when passed charset" do
|
||||
route = Route.new "POST", "/" { }
|
||||
|
||||
request = HTTP::Request.new(
|
||||
"POST",
|
||||
"/",
|
||||
body: "{\"name\": \"Serdar\"}",
|
||||
headers: HTTP::Headers{"Content-Type" => "application/json; charset=utf-8"},
|
||||
)
|
||||
|
||||
json_params = Kemal::ParamParser.new(request).json
|
||||
json_params.should eq({"name" => "Serdar"})
|
||||
end
|
||||
|
||||
it "parses request body for array" do
|
||||
route = Route.new "POST", "/" { }
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ module Kemal
|
|||
# If request body is a JSON Array it's added into `params` as `_json` and can be accessed
|
||||
# like params["_json"]
|
||||
def parse_json
|
||||
return unless @request.body && @request.headers["Content-Type"]? == APPLICATION_JSON
|
||||
return unless @request.body && @request.headers["Content-Type"]?.try(&.[APPLICATION_JSON]?)
|
||||
|
||||
body = @request.body.as(String)
|
||||
case json = JSON.parse(body).raw
|
||||
|
|
Loading…
Reference in a new issue