Github iconEdit on Github
This version of the Cloudflare Workers documentation is deprecated. Visit the new documentation

Setting a Cookie

You can set cookies using Cloudflare Workers.

addEventListener('fetch', event => {

async function fetchAndApply(request) {
  let response = await fetch(request)

  const randomStuff = `randomcookie=${Math.random()}; Expires=Wed, 21 Oct 2018 07:28:00 GMT; Path='/';`

  // Make the headers mutable by re-constructing the Response.
  response = new Response(response.body, response)
  response.headers.set('Set-Cookie', randomStuff)

  return response