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

Issue with Header Capitalization Affecting API Requests

Open juancarlis opened this issue 1 year ago • 2 comments

Hello,

I am using rest.nvim for making API requests and have encountered an issue related to the capitalization of HTTP headers. Specifically, the issue arises with the Api-Version header.

When I make a request using rest.nvim, the header Api-Version is automatically capitalized. However, the API I am interacting with is case-sensitive and expects the header in lowercase (api-version). This discrepancy is causing my requests to fail with a 400 error.

Here's an example of the request made by rest.nvim:

curl -sSL --compressed -X 'POST' -H 'Api-Version: 9' -H 'Accept: */*' -H 'Content-Type: application/json;odata.metadata=minimal;odata.streaming=true' --data-raw '{"clientId": "", "password": "somepassword*", "userName": "someusername"}' -k 'http://1.1.1.1:1000/api/v9/login'

And here's how I need it to be for the request to succeed:

curl -sSL --compressed -X 'POST' -H 'api-version: 9' -H 'Accept: */*' -H 'Content-Type: application/json;odata.metadata=minimal;odata.streaming=true' --data-raw '{"clientId": "", "password": "somepassword*", "userName": "someusername"}' -k 'http://1.1.1.1:1000/api/v9/login'

I have tried various configurations within rest.nvim, but it seems that there is no option available to control or prevent the capitalization of headers.

Is there a workaround or a potential fix that could be implemented to address this issue? Any assistance or guidance on this matter would be greatly appreciated.

Thank you for your time and for developing rest.nvim.

juancarlis avatar Dec 05 '23 19:12 juancarlis

I've investigated further and found that the capitalization behavior is originating from the plenary.curl module, specifically in the parse.headers function. This function was automatically capitalizing the headers, which led to the issue with my API requests.

To resolve this, I made a modification in the parse.headers function of plenary.curl. Specifically, I adjusted the upper function within parse.headers to return the header string as is, without changing its capitalization:

parse.headers = function(t)
  if not t then
    return
  end
  local upper = function(str)
    return str
  end
  return util.kv_to_list(
    (function()
      local normilzed = {}
      for k, v in pairs(t) do
        normilzed[upper(k:gsub("_", "%-"))] = v
      end
      return normilzed
    end)(),
    "-H",
    ": "
  )
end

With this modification, rest.nvim now sends the headers exactly as I specify them, preserving the original case, which resolved the issue with my API that is case-sensitive to headers.

I wanted to share this solution in case it's helpful for others who might encounter a similar issue or if it's something that could be considered for future updates to rest.nvim or plenary.curl.

juancarlis avatar Dec 07 '23 11:12 juancarlis

Hey, I think this is something that must be reported and patched in plenary.nvim's repository instead. Once it is fixed there it will be also fixed here :)

NTBBloodbath avatar Dec 08 '23 20:12 NTBBloodbath