Use String#starts_with? instead of String#[]? (#194)

It shows real intention and covers cases when `application/json` is passed in the middle of the string, like `some_garbage_string; application/json`
This commit is contained in:
Sijawusz Pur Rahnama 2016-08-12 21:17:57 +02:00 committed by Serdar Dogruyol
parent 28859fbdb8
commit a6678f6622

View file

@ -55,7 +55,7 @@ module Kemal
# If request body is a JSON Array it's added into `params` as `_json` and can be accessed # If request body is a JSON Array it's added into `params` as `_json` and can be accessed
# like params["_json"] # like params["_json"]
def parse_json def parse_json
return unless @request.body && @request.headers["Content-Type"]?.try(&.[APPLICATION_JSON]?) return unless @request.body && @request.headers["Content-Type"]?.try(&.starts_with?(APPLICATION_JSON))
body = @request.body.as(String) body = @request.body.as(String)
case json = JSON.parse(body).raw case json = JSON.parse(body).raw