rest.nvim
rest.nvim copied to clipboard
Touches names of Header values
My Rest API requires me to specify a header value of TOKEN: <token>
with the name of the header value in all-upper-case. When executing the request the API always complains of not having a token in the header.... checking the cUrl command that is issued it seems the plugin changes the the header name from all upper-case to pascal case...
GET https://test/me
**TOKEN**: {{TOKEN}}
Accepts: application/json
result in this cUrl:
curl -sSL --compressed -X 'GET' -H '**Token**: ...' -H 'Accepts: application/json' 'https://test/me'
If it’s related to this line: https://github.com/rest-nvim/rest.nvim/blob/main/lua/rest-nvim/request/init.lua#L124 I might be able to provide a PR for it tomorrow…
Ok, made a small local improvement on the above mentioned code line. But stumbled upon another one:
If you specify API_TOKEN: <token>
as header the cUrl command will send a API-TOKEN: <token>
instead. This was a bit harder to figure out, but looks like its an issue in the plenary: https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/curl.lua#L95
Reopened this issue until rest.nvim codebase rewrite is done as there's another bug you've mentioned that is related to plenary and codebase rewrite should be able to fix this behavior.
Hi @NTBBloodbath , it seems PR https://github.com/rest-nvim/rest.nvim/pull/142 broke Content-Type: application/json
header, only content-type: application/json
works fine now.
The code here https://github.com/rest-nvim/rest.nvim/blob/main/lua/rest-nvim/request/init.lua#L270 checks 'content-type' from header.
I dont know why we special case json (maybe to catch errors early on ?), it justs decode then reencode the content :s If i remove the special casing it just works !
diff --git a/lua/rest-nvim/request/init.lua b/lua/rest-nvim/request/init.lua
index 9d7727b..905efd7 100644
--- a/lua/rest-nvim/request/init.lua
+++ b/lua/rest-nvim/request/init.lua
@@ -41,8 +41,7 @@ end
-- @param bufnr Buffer number, a.k.a id
-- @param start_line Line where body starts
-- @param stop_line Line where body stops
--- @param has_json True if content-type is set to json
-local function get_body(bufnr, start_line, stop_line, has_json)
+local function get_body(bufnr, start_line, stop_line)
-- first check if the body should be imported from an external file
local importfile = get_importfile_name(bufnr, start_line, stop_line)
local lines
@@ -67,23 +66,6 @@ local function get_body(bufnr, start_line, stop_line, has_json)
end
end
- local is_json, json_body = pcall(vim.fn.json_decode, body)
-
- if is_json then
- if has_json then
- -- convert entire json body to string.
- return vim.fn.json_encode(json_body)
- else
- -- convert nested tables to string.
- for key, val in pairs(json_body) do
- if type(val) == "table" then
- json_body[key] = vim.fn.json_encode(val)
- end
- end
- return json_body
- end
- end
-
return body
end