mistweaverco / kulala.nvim

A minimal 🤏 HTTP-client 🐼 interface 🖥️ for Neovim ❤️.
https://kulala.mwco.app
MIT License
682 stars 34 forks source link

Collsion in response and request headers #281

Open Norbytus opened 1 month ago

Norbytus commented 1 month ago

If response or request have header with same name, response.headers.valuesOf('Header name') return last header

gorillamoe commented 1 month ago

Can you give me an example on this?

Because it works as intended, afaik:

# @name REQUEST_FOOBAR
POST https://httpbin.org/post HTTP/1.1
Date: Foobar

{
  "foo": "bonobo",
  "foobar": true
}

> {%
client.log(request.headers.findByName("Date").getRawValue());
client.log(response.headers.valuesOf("Date").value);
%}

This outputs:

Foobar
Mon, 14 Oct 2024 01:27:03 GMT

The first one is the header from the request and the second one the header from the response.

Norbytus commented 1 month ago

Its more i wrote wrong description, For example you have in response or request headers like

HTTP/1.1 200 OK
Cache-Control: no-cache, private
Content-Type: application/json
Set-Cookie: Access-Token=value
Set-Cookie: Refresh-Token=value
Vary: Accept-Encoding
Transfer-Encoding: chunked

When i try get access to header you get last one response.headers.valuesOf('Set-Cookie')

Grueslayer commented 1 month ago

RFC 2616 says it is possible to send multiple values in separate header lines only when it is possible to also combine the values by comma in one header line. Currently it is not supported in Kulala.

Some implementation always return an array, some will combine these to a comma separated list (which I think is the only solution to have backward compatibility)

Norbytus commented 1 month ago

In jetbrains it's looks like

 response.headers.valuesOf('Set-Cookie').forEach(function (value) {/*... */}
gorillamoe commented 1 month ago

In jetbrains it's looks like

 response.headers.valuesOf('Set-Cookie').forEach(function (value) {/*... */}

Will try in jetbrains and see if we can implement it in kulala.

Grueslayer commented 1 month ago

What is the result when there is only one occurrence? A string (so the caller has to check the result type) or an array with one element (that would be a breaking change in scripts).

we need to decide what we do with the Request Variables: vscode-rest-client returns the first one.

As RFC defines all implementations must accept both forms maybe we return the comma list at least in Request Variables.

if you test against JetBrains please also check if they deliver an array if given as list in one header.

Norbytus commented 1 month ago

I don't use JetBrains, but my college use like this JetBrains doc https://www.jetbrains.com/help/idea/http-response-reference.html#headers-reference

 response.headers.valuesOf('Set-Cookie').forEach(function (value) {
        const parts = value.split('=');
        const settings = parts[1].split(';');

        if (parts[0] === 'First_Variant') {
/* ... */
        }

        if (parts[0] === 'Second_Varoant') {
/* .. */
        }
    });