Get request headers from received request in fetch event.

I am trying to get the headers of received requests, but end up getting null for the following code:
export default {
async fetch(request, env, ctx) {
console.log(JSON.stringify(request.headers));
}
}
export default {
async fetch(request, env, ctx) {
console.log(JSON.stringify(request.headers));
}
}
3 Replies
kian
kian2y ago
export default {
fetch(req) {
return Response.json(Object.fromEntries(req.headers.entries()));
},
};
export default {
fetch(req) {
return Response.json(Object.fromEntries(req.headers.entries()));
},
};
salad
saladOP2y ago
oh, so it's req.headers.entries()? thanks
kian
kian2y ago
Entries returns an iterator of key/value pairs (https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries) Object.fromEntries turns that into an object which you can then stringify - or generally do whatever with
Want results from more Discord servers?
Add your server