302 Redirect headers are missing

Is it possible to extract the original headers from a worker fetch request to an end-point which returns a 302? e.g. const res = await fetch("http://localhost:3000/", { "redirect": "manual" }) res = {"body":{},"bodyUsed":false,"headers":{},"url":"http://localhost:3000/","redirected":false,"ok":false,"statusText":"Found","status":302} If I run the same request via curl I get the original headers (e.g. location + set-cookie): curl -v http://localhost:3000/ < HTTP/2 302 < cache-control: private < location: https://www.google.com < set-cookie: SIGNIN.REQUEST=1683699853; path=/; secure; HttpOnly < access-control-expose-headers: Request-Context < date: Tue, 09 May 2023 14:49:59 GMT < content-length: 0 Is it possible to extract the original headers using Cloudflare workers (or any other Cloudflare service)? The main problem is that the set-cookie header is lost.
Google
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.
7 Replies
James
James2y ago
The headers shouldn't be lost - where are you seeing that? If you're only looking at the log of res, you won't see headers due to how that gets serialised You'll want to const headers = Object.fromEntries(res.headers.entries()) or something Or just res.headers.get('location'), etc.
DrKrall
DrKrallOP2y ago
I agree, but they are. The 302 response object only contains following properties. {"body":{},"bodyUsed":false,"headers":{},"url":"http://localhost:3000/", "redirected":false,"ok":false,"statusText":"Found","status":302} I think there is a limitation / restriction with fetch and response code 302. Is there any workaround?
James
James2y ago
Where is that log from? Headers being empty is a misnomer - it’s not actually empty. That’s a result of how things get serialised for logs. Try this, and log the object
DrKrall
DrKrallOP2y ago
Thanks! It worked with Object.fromEntries(res.headers.entries()), but console.log("Headers", JSON.stringify(res.headers.entries())) contains nothing
James
James2y ago
Yep entries is an iterator so you have to consume it first, then you can log it 😀 If you only need one header you can also just do this ^
DrKrall
DrKrallOP2y ago
Many thanks James! You saved my day 🙂
James
James2y ago
Happy to help!
Want results from more Discord servers?
Add your server