Alter headers
Example of how to add, change, or delete headers sent in a request or returned in a response.
export default {async fetch(request) {const response = await fetch(request);// Clone the response so that it's no longer immutableconst newResponse = new Response(response.body, response);// Add a custom header with a valuenewResponse.headers.append("x-workers-hello","Hello from Cloudflare Workers");// Delete headersnewResponse.headers.delete("x-header-to-delete");newResponse.headers.delete("x-header2-to-delete");// Adjust the value for an existing headernewResponse.headers.set("x-header-to-change", "NewValue");return newResponse;},};
const handler: ExportedHandler = {async fetch(request: Request) {const response = await fetch(request);// Clone the response so that it's no longer immutableconst newResponse = new Response(response.body, response);// Add a custom header with a valuenewResponse.headers.append("x-workers-hello","Hello from Cloudflare Workers");// Delete headersnewResponse.headers.delete("x-header-to-delete");newResponse.headers.delete("x-header2-to-delete");// Adjust the value for an existing headernewResponse.headers.set("x-header-to-change", "NewValue");return newResponse;},};export default handler;
You can also use the
custom-headers-example
template to deploy this code to your custom domain.