Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix res:json() is nil caused by content-type detect method #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/resty/requests/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local util = require "resty.requests.util"

local is_tab = util.is_tab
local new_tab = util.new_tab
local includes = util.includes
local find = string.find
local lower = string.lower
local insert = table.insert
Expand Down Expand Up @@ -312,9 +313,7 @@ local function json(r)
end

content_type = lower(content_type)
if content_type ~= "application/json"
and content_type ~= "application/json; charset=utf-8"
then
if not includes(content_type, "application/json") then
Copy link
Owner

Choose a reason for hiding this comment

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

Just use string.find is enough to satisfy this demand.

Copy link
Author

Choose a reason for hiding this comment

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

It seems you are right. See the issue first #29 , i have left some messages.

return nil, "not json"
end

Expand Down
11 changes: 11 additions & 0 deletions lib/resty/requests/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local rawget = rawget
local setmetatable = setmetatable
local lower = string.lower
local ngx_gsub = ngx.re.gsub
local ngx_re_match = ngx.re.match
local base64 = ngx.encode_base64

local _M = { _VERSION = '0.1' }
Expand Down Expand Up @@ -191,12 +192,22 @@ local function set_config(opts)
return config
end

local function includes(text, key_word)
local matched, err = ngx_re_match(text, key_word, "jo")

if not matched or err then
return false
end

return true
end

_M.new_tab = new_tab
_M.is_str = is_str
_M.is_num = is_num
_M.is_tab = is_tab
_M.is_func = is_func
_M.includes = includes
_M.set_config = set_config
_M.dict = dict
_M.basic_auth = basic_auth
Expand Down