parse request body for array
This commit is contained in:
parent
d280dd7f87
commit
51708ce8e7
2 changed files with 37 additions and 3 deletions
|
@ -45,6 +45,34 @@ describe "ParamParser" do
|
||||||
params.should eq({"name": "Serdar"})
|
params.should eq({"name": "Serdar"})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "parses request body for array" do
|
||||||
|
route = Route.new "POST", "/" { }
|
||||||
|
|
||||||
|
request = HTTP::Request.new(
|
||||||
|
"POST",
|
||||||
|
"/",
|
||||||
|
body: "[1]",
|
||||||
|
headers: HTTP::Headers{"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
|
||||||
|
params = Kemal::ParamParser.new(route, request).parse
|
||||||
|
params.should eq({"_json": [1]})
|
||||||
|
end
|
||||||
|
|
||||||
|
it "parses request body and query params" do
|
||||||
|
route = Route.new "POST", "/" { }
|
||||||
|
|
||||||
|
request = HTTP::Request.new(
|
||||||
|
"POST",
|
||||||
|
"/?foo=bar",
|
||||||
|
body: "[1]",
|
||||||
|
headers: HTTP::Headers{"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
|
||||||
|
params = Kemal::ParamParser.new(route, request).parse
|
||||||
|
params.should eq({"foo": "bar", "_json": [1]})
|
||||||
|
end
|
||||||
|
|
||||||
it "handles no request body" do
|
it "handles no request body" do
|
||||||
route = Route.new "GET", "/" { }
|
route = Route.new "GET", "/" { }
|
||||||
|
|
||||||
|
|
|
@ -39,11 +39,17 @@ class Kemal::ParamParser
|
||||||
|
|
||||||
def parse_json
|
def parse_json
|
||||||
return unless @request.body && @request.headers["Content-Type"]? == APPLICATION_JSON
|
return unless @request.body && @request.headers["Content-Type"]? == APPLICATION_JSON
|
||||||
|
|
||||||
body = @request.body as String
|
body = @request.body as String
|
||||||
json = JSON.parse(body) as Hash
|
|
||||||
|
case json = JSON.parse(body)
|
||||||
|
when Hash
|
||||||
json.each do |k, v|
|
json.each do |k, v|
|
||||||
@params[k as String] = v as AllParamTypes
|
@params[k as String] = v as AllParamTypes
|
||||||
end
|
end
|
||||||
|
when Array
|
||||||
|
@params["_json"] = json
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_part(part)
|
def parse_part(part)
|
||||||
|
|
Loading…
Reference in a new issue