Request headers not available in workers

Is access to event.request.headers limited in workers?

To illustrate, I’ve got this simple worker basically logging the request headers:

addEventListener("fetch", (event) => {
  event.respondWith(
    handleRequest(event.request).catch(
      (err) => new Response(err.stack, { status: 500 })
    )
  );
});

async function handleRequest(request) {
  console.log(new Map(request.headers))
  return fetch("https://welcome.developers.workers.dev");
}

The weird thing is that it works as expected here: https://cloudflareworkers.com/#496e8336d13db706cc66f17f655cce7d:https://tutorial.cloudflareworkers.com, but not in a real worker.
In the real worker only host and x-forwarded-proto is logged. From docs and other examples, it seems like more headers should be available… I need to use the header values for logic in my workers.

FWIW, I’m on a paid plan.

1 Like

console.log() expects a string. You’re passing a map so the output will be unpredictable.

Could you please try using console.log(JSON.stringify(Object.fromEntries(event.request.headers)))?

Thanks. The web UI at cloudflareworkers.com logs it nicely in the console actually. but you’re right, not in any other place.
Still that doesn’t solve my problem… I’m trying the wrangler way, now.

I found that using miniflare locally is great for logging and debugging purposes