-2

This a re-post since my original "question-answer" were deleted in this directly-related thread.

How do you read "merged" multi cookies from Fetch API, especially when containing Expiration dates ?

We tried with request.headers.get('cookie').split(',') where :

Cookie: cookie1=value1; Path=/; Expires=Tue, 04 Aug 2020 19:45:53 GMT; HttpOnly, cookie2=value2; Path=/; Expires=Tue, 05 Sept 2020 14:32:03 GMT; HttpOnly

... but obviously this fails because of the comma in the dates.

Context: i'm trying to promote native fetch to my teams currently using XMLHttpRequests, but they are reluctant because of this -very frequent- expires issue.

Sharlaan
  • 51
  • 9
  • 1
    The dupes are from 2011 and 2012 but still applies – mplungjan Mar 20 '23 at 08:23
  • they don't applies at all (or did not find anything related - also fetch did not exist in 2011/2012) i'm asking specifically about multi-cookies from fetch **containing dates with comma** – Sharlaan Mar 20 '23 at 10:33
  • No, You ask about parsing a cookie. It should not be comma delimited. You likely get a toString of an array or something. – mplungjan Mar 20 '23 at 12:14
  • What does `console.log(response.headers.get('set-cookie'))` give you after you fetch with `{ headers: { credentials: "include" } }` – mplungjan Mar 20 '23 at 13:32
  • As explained in previous thread, and explicitly stated here, parsing a multi-cookies from Fetch results in a single string with ALL cookies concatenated. Thing i'm asking is how to parse/read in the particular case -and extremely frequent- where an expires date is set. We are stuck with old fashioned XMLHttpRequest, i'm trying to promote fetch, but because of this issue, team is reluctant to hop-in.... in 2023 o.O – Sharlaan Mar 20 '23 at 14:03
  • Ok. Using cookies in 2023 is also so 2001 ;) I have reopened the question – mplungjan Mar 20 '23 at 14:06
  • Also when browser is auto-setting all requests with Set-Cookie valueS from backend, there are consequently only one single Cookie header (containing all subcookies concatenated) when backend receive any request, no "Set-Cookie" – Sharlaan Mar 20 '23 at 14:09

1 Answers1

0

Does this work for you?

const cookie =`cookie1=value1; Path=/; Expires=Tue, 04 Aug 2020 19:45:53 GMT; HttpOnly, cookie2=value2; Path=/; Expires=Tue, 05 Sept 2020 14:32:03 GMT; HttpOnly`;
const parts = [...cookie.matchAll(/(.*?); Path=(.*?); Expires=(.*?); HttpOnly,? ?/gm)]

parts.forEach(part => {
  const [_,keyVal,path,date] = part;
  console.log(keyVal.split("="))
})
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • At first glance yes (need to get mostly the expires value to check validity) ... but actually might be fragile for example when cookie properties are unordered or missing ? – Sharlaan Mar 20 '23 at 15:19
  • They are unlikely to be unordered. You can implement an increasingly complex parsing in this order: `name,value, expiry-time, domain, path, creation-time, last-access-time, persistent-flag, host-only-flag, secure-only-flag, and http-only- flag.` – mplungjan Mar 20 '23 at 15:24