Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

ヘッダーを変更する

リクエストで送るヘッダー、またはレスポンスで返すヘッダーを追加、変更、削除する例です。

最終更新 Markdown で表示Agent セットアップ

すぐに始める場合は、下のボタンを選択します。

Cloudflare にデプロイ

GitHub アカウントにリポジトリが作成され、アプリケーションが Cloudflare Workers にデプロイされます。

export default {
	async fetch(request) {
		const response = await fetch("https://example.com");

		// Clone the response so that it's no longer immutable
		const newResponse = new Response(response.body, response);

		// Add a custom header with a value
		newResponse.headers.append(
			"x-workers-hello",
			"Hello from Cloudflare Workers",
		);

		// Delete headers
		newResponse.headers.delete("x-header-to-delete");
		newResponse.headers.delete("x-header2-to-delete");

		// Adjust the value for an existing header
		newResponse.headers.set("x-header-to-change", "NewValue");

		return newResponse;
	},
};
export default {
	async fetch(request): Promise<Response> {
		const response = await fetch(request);

		// Clone the response so that it's no longer immutable
		const newResponse = new Response(response.body, response);

		// Add a custom header with a value
		newResponse.headers.append(
			"x-workers-hello",
			"Hello from Cloudflare Workers",
		);

		// Delete headers
		newResponse.headers.delete("x-header-to-delete");
		newResponse.headers.delete("x-header2-to-delete");

		// Adjust the value for an existing header
		newResponse.headers.set("x-header-to-change", "NewValue");

		return newResponse;
	},
} satisfies ExportedHandler;
from workers import Response, fetch, WorkerEntrypoint

class Default(WorkerEntrypoint):
  async def fetch(self, request):
      response = await fetch("https://example.com")

      # Grab the response headers so they can be modified
      new_headers = response.headers

      # Add a custom header with a value
      new_headers["x-workers-hello"] = "Hello from Cloudflare Workers"

      # Delete headers
      if "x-header-to-delete" in new_headers:
          del new_headers["x-header-to-delete"]
      if "x-header2-to-delete" in new_headers:
          del new_headers["x-header2-to-delete"]

      # Adjust the value for an existing header
      new_headers["x-header-to-change"] = "NewValue"

      return Response(response.body, headers=new_headers)
import { Hono } from 'hono';

const app = new Hono();

app.use('*', async (c, next) => {
  // Process the request with the next middleware/handler
  await next();

  // After the response is generated, we can modify its headers

  // Add a custom header with a value
  c.res.headers.append(
    "x-workers-hello",
    "Hello from Cloudflare Workers with Hono"
  );

  // Delete headers
  c.res.headers.delete("x-header-to-delete");
  c.res.headers.delete("x-header2-to-delete");

  // Adjust the value for an existing header
  c.res.headers.set("x-header-to-change", "NewValue");
});

app.get('*', async (c) => {
  // Fetch content from example.com
  const response = await fetch("https://example.com");

  // Return the response body with original headers
  // (our middleware will modify the headers before sending)
  return new Response(response.body, {
    headers: response.headers
  });
});

export default app;

このコードをカスタムドメインにデプロイするには、custom-headers-example テンプレート も使えます。

役に立ちましたか?