rest.nvim icon indicating copy to clipboard operation
rest.nvim copied to clipboard

Touches names of Header values

Open appelgriebsch opened this issue 2 years ago • 5 comments

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'

appelgriebsch avatar Oct 11 '22 13:10 appelgriebsch

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…

appelgriebsch avatar Oct 11 '22 18:10 appelgriebsch

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

appelgriebsch avatar Oct 13 '22 08:10 appelgriebsch

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.

NTBBloodbath avatar Nov 25 '22 22:11 NTBBloodbath

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.

rhinoxi avatar Nov 29 '22 09:11 rhinoxi

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

teto avatar Dec 29 '22 18:12 teto