As you might know, RFC 6265 indicates that it is allowed to have multiple headers with the Set-Cookie
name.
However, Fetch API doesn't allow to do that because all the methods exposed by its Headers interface (including get()
, set()
, append()
, entries()
and all the rest) have been implemented to merge the values of all the headers with the same name into a single header separated by commas.
For example, if we do this:
var headers = new Headers();
headers.append('content-type', 'text/plain');
headers.append('set-cookie', 'test1=v; Max-Age=0');
headers.append('set-cookie', 'test2=v; Max-Age=0');
headers.append('set-cookie', 'test3=v; Max-Age=0');
and then we try to read the set-cookie
values using get('set-cookie')
, or by iterating the headers
variable using entries()
, we get this:
'set-cookie' : test1=v; Max-Age=0, test2=v; Max-Age=0, test3=v; Max-Age=0
Please notice that the same wrong behaviour also happens if we try to read or manipulate an existing response object having multiple headers with the same name (i.e. created by other frameworks that arguably support such allowed behavior): in other words, it seems like the Fetch API
is completely unable to properly deal with such scenario.
Now, while this behavior is desired for some headers, such as Accept
, the Set-Cookie
header is not parsed correctly by most browsers (including Chrome and Firefox), thus resulting in cookies not being correctly set.
Is that a known bug? If that's the case, is there an usable workaround that can be used to overcome this?
.entries()
) – FavouriteSet-Cookie
is sent by the server, not the client. Why do you need it in the Fetch API? – Nannienanning