From 10ad5b77bed3e23e7d3e3b7073dc984ba9b6c826 Mon Sep 17 00:00:00 2001 From: whatwewant Date: Thu, 3 Oct 2019 22:26:54 +0800 Subject: [PATCH] bugfix: fix res:json() is nil caused by content-type detect method --- lib/resty/requests/response.lua | 5 ++--- lib/resty/requests/util.lua | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/resty/requests/response.lua b/lib/resty/requests/response.lua index 5e68ed2..fd9bf55 100644 --- a/lib/resty/requests/response.lua +++ b/lib/resty/requests/response.lua @@ -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 @@ -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 return nil, "not json" end diff --git a/lib/resty/requests/util.lua b/lib/resty/requests/util.lua index 33a02fa..a11b933 100644 --- a/lib/resty/requests/util.lua +++ b/lib/resty/requests/util.lua @@ -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' } @@ -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