When making a fetch request to an external service from a Worker, certain request headers are populated by default. The Workers runtime and miniflare send different default headers, meaning that when calling some external APIs (e.g. Github's which requires a User-Agent header for all requests!) code that works in miniflare will fail when running on the Workers runtime.
To validate this, I set up a simple echo service running on https://http-ua-proof-of-concept.dancowell.workers.dev/ which will return a structured JSON object containing request data.
Executing the following worker in each context results in different output:
export async function handleRequest(request: Request, env: Bindings) {
const response = await fetch(
"https://http-ua-proof-of-concept.dancowell.workers.dev/"
);
const body = await response.text();
return new Response(body);
}
const worker: ExportedHandler<Bindings> = { fetch: handleRequest };
export default worker;
Request from miniflare
{
"method": "GET",
"url": "https://http-ua-proof-of-concept.dancowell.workers.dev/",
"body": "",
"headers": {
"accept": "*/*",
"accept-encoding": "gzip",
"accept-language": "*",
"cf-connecting-ip": "126.124.253.198",
"cf-ipcountry": "JP",
"cf-ray": "6c6fcc948c918d01",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Keep-Alive",
"host": "http-ua-proof-of-concept.dancowell.workers.dev",
"sec-fetch-mode": "cors",
"user-agent": "undici",
"x-forwarded-proto": "https",
"x-real-ip": "126.124.253.198"
}
}
Request from Cloudflare
{
"method": "GET",
"url": "https://http-ua-proof-of-concept.dancowell.workers.dev/",
"body": "",
"headers": {
"accept-encoding": "gzip",
"cf-connecting-ip": "35.224.27.5",
"cf-ipcountry": "US",
"cf-ray": "6c6fdbd42d216318",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Keep-Alive",
"host": "http-ua-proof-of-concept.dancowell.workers.dev",
"x-forwarded-proto": "https",
"x-real-ip": "35.224.27.5"
}
}
Based on this output, miniflare sends 4 additional headers by default: accept, accept-language, sec-fetch-mode, user-agent
I would expect that miniflare's default request headers should match that of the workers runtime.
When making a
fetchrequest to an external service from a Worker, certain request headers are populated by default. The Workers runtime and miniflare send different default headers, meaning that when calling some external APIs (e.g. Github's which requires aUser-Agentheader for all requests!) code that works in miniflare will fail when running on the Workers runtime.To validate this, I set up a simple echo service running on https://http-ua-proof-of-concept.dancowell.workers.dev/ which will return a structured JSON object containing request data.
Executing the following worker in each context results in different output:
Request from miniflare
Request from Cloudflare
Based on this output, miniflare sends 4 additional headers by default:
accept, accept-language, sec-fetch-mode, user-agentI would expect that miniflare's default request headers should match that of the workers runtime.