diff --git a/lua/rest-nvim/curl/init.lua b/lua/rest-nvim/curl/init.lua index 7b3bfd6b..d0d928a1 100644 --- a/lua/rest-nvim/curl/init.lua +++ b/lua/rest-nvim/curl/init.lua @@ -103,14 +103,9 @@ local function create_callback(curl_cmd, opts) return end local res_bufnr = M.get_or_create_buf() - local content_type = nil - - -- get content type - for _, header in ipairs(res.headers) do - if string.lower(header):find("^content%-type") then - content_type = header:match("application/([-a-z]+)") or header:match("text/(%l+)") - break - end + local content_type = res.headers[utils.key(res.headers,'content-type')] + if content_type then + content_type = content_type:match("application/([-a-z]+)") or content_type:match("text/(%l+)") end if script_str ~= nil then diff --git a/lua/rest-nvim/init.lua b/lua/rest-nvim/init.lua index edc0a4d8..fe94261d 100644 --- a/lua/rest-nvim/init.lua +++ b/lua/rest-nvim/init.lua @@ -101,13 +101,7 @@ local function splice_body(headers, payload) else lines = payload.body_tpl end - local content_type = "" - for key, val in pairs(headers) do - if string.lower(key) == "content-type" then - content_type = val - break - end - end + local content_type = headers[utils.key(headers,"content-type")] or "" local has_json = content_type:find("application/[^ ]*json") local body = "" diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua index 0f1fdc5c..6b071b11 100644 --- a/lua/rest-nvim/request/init.lua +++ b/lua/rest-nvim/request/init.lua @@ -286,12 +286,9 @@ M.buf_get_request = function(bufnr, curpos) local curl_args, body_start = get_curl_args(bufnr, headers_end, end_line) - if headers["host"] ~= nil then - headers["host"] = headers["host"]:gsub("%s+", "") - headers["host"] = string.gsub(headers["host"], "%s+", "") - parsed_url.url = headers["host"] .. parsed_url.url - headers["host"] = nil - end + local host = headers[utils.key(headers,"host")] or "" + parsed_url.url = host:gsub("%s+", "") .. parsed_url.url + headers[utils.key(headers,"host")] = nil local body = get_body(bufnr, body_start, end_line) diff --git a/lua/rest-nvim/utils/init.lua b/lua/rest-nvim/utils/init.lua index 2c8393ac..99fa73cc 100644 --- a/lua/rest-nvim/utils/init.lua +++ b/lua/rest-nvim/utils/init.lua @@ -107,6 +107,7 @@ M.get_file_variables = function() end return variables end + -- Gets the variables from the currently selected env_file M.get_env_variables = function() local variables = {} @@ -286,6 +287,19 @@ M.has_value = function(tbl, str) return false end +-- key returns the provided table's key that matches the given case-insensitive pattern. +-- if not found, return the given key. +-- @param tbl Table to iterate over +-- @param key The key to be searched in the table +M.key = function(tbl, key) + for tbl_key, _ in pairs(tbl) do + if string.lower(tbl_key) == string.lower(key) then + return tbl_key + end + end + return key +end + -- tbl_to_str recursively converts the provided table into a json string -- @param tbl Table to convert into a String -- @param json If the string should use a key:value syntax diff --git a/tests/get_with_host.http b/tests/get_with_host.http index 0e66b253..a76c11bd 100644 --- a/tests/get_with_host.http +++ b/tests/get_with_host.http @@ -1,7 +1,7 @@ ### GET /api/users?page=5 -Host: https://reqres.in +host: https://reqres.in ### diff --git a/tests/main_spec.lua b/tests/main_spec.lua index dc4accfe..2f35f4bc 100644 --- a/tests/main_spec.lua +++ b/tests/main_spec.lua @@ -6,6 +6,7 @@ describe("rest testing framework", function() assert(rest.run_file("tests/basic_get.http", opts) == true) assert(rest.run_file("tests/post_json_form.http", opts) == true) assert(rest.run_file("tests/post_create_user.http", opts) == true) + assert(rest.run_file("tests/get_with_host.http", opts) == true) assert(rest.run_file("tests/put_update_user.http", opts) == true) assert(rest.run_file("tests/patch_update_user.http", opts) == true) assert(rest.run_file("tests/delete.http", opts) == true)