All Products
Search
Document Center

Dynamic Content Delivery Network:Modify headers

Last Updated:Oct 18, 2023

This topic provides sample code that shows how to modify headers.

Code


/**
 * In this example, https://demo.aliyundoc.com/index.html is requested, response headers are modified, and the response headers are returned to the client.
 * Replace someHost and url with your host and URL addresses during the test.
 */
const someHost = "https://demo.aliyundoc.com"
const url = someHost + "/index.html"

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

async function handleRequest(request) {
  const response = await fetch(url)

  // Copy the body of the response.
  const newResponse = new Response(response.body, response)

  // Add headers.
  newResponse.headers.append("custom-ER-add", "ER header")

  // Remove headers.
  newResponse.headers.delete("custom-ER-delete")
  newResponse.headers.delete("custom2-ER-delete")

  // Modify headers.
  newResponse.headers.set("custom-ER-reset", "ER header")

  return newResponse
}

Result

EdgeRoutine copies the body of the response, adds headers, removes headers, or modifies headers, and returns the final request to the client.

0