Worker Injected Origin headers add `HTTP-` to all key names

I have a new worker that i use to inject geolocation headers to the origin call. the code looks like this

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const requestWithRegion = new Request(request);
  requestWithRegion.headers.set('x-region-code', request.cf?.regionCode);
  requestWithRegion.headers.set('x-region', request.cf?.region);
  requestWithRegion.headers.set('x-postal-code', request.cf?.postalCode);
  requestWithRegion.headers.set('NOCACHE', 1);
  return await fetch(requestWithRegion);
}

The issue is that these headers come out as HTTP_X_REGION_CODE, HTTP_X_REGION, HTTP_X_POSTAL_CODE, HTTP_NOCACHE.

For all of the geolocation stuff that is fine but i need HTTP_NOCACHE to drop the HTTP_ and just be NOCACHE to prevent my origin application from using the caching it has in place. Before you bother suggesting, I cannot alter the origin app. I must send exactly NOCACHE as a header value.

Any suggestions?

Where are you seeing the HTTP_ prefix? This doesn’t sound like something Workers is doing, and those headers are absolutely going to be sent exactly as you have defined them, like x-region-code, etc.

Is it possible you’re seeing this in PHP’s $_SERVER? This is simply how PHP (via CGI) represents HTTP headers; any element in $_SERVER that starts with HTTP_ is from a request header, like HTTP_HOST maps to the host header, for example.

1 Like

OOOOOOOOhhhh SNAP!!!

that is EXACTLY what it is. I am seeing that in the

var_dump($_SERVER)

that I am using to debug. :man_facepalming:t3: I forgot about that!!! Thanks.

1 Like

Thank you @cherryjimbo

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.