Skip to content

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

Cookie の解析

Cookie 名を指定して、その値を取得します。Cookie を A/B テストに使うこともできます。

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

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

Cloudflare にデプロイ

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

import { parseCookie } from "cookie";
export default {
	async fetch(request) {
		// The name of the cookie
		const COOKIE_NAME = "__uid";
		const cookie = parseCookie(request.headers.get("Cookie") || "");
		if (cookie[COOKIE_NAME] != null) {
			// Respond with the cookie value
			return new Response(cookie[COOKIE_NAME]);
		}
		return new Response("No cookie with name: " + COOKIE_NAME);
	},
};
import { parseCookie } from "cookie";
export default {
	async fetch(request): Promise<Response> {
		// The name of the cookie
		const COOKIE_NAME = "__uid";
		const cookie = parseCookie(request.headers.get("Cookie") || "");
		if (cookie[COOKIE_NAME] != null) {
			// Respond with the cookie value
			return new Response(cookie[COOKIE_NAME]);
		}
		return new Response("No cookie with name: " + COOKIE_NAME);
	},
} satisfies ExportedHandler;
from http.cookies import SimpleCookie
from workers import WorkerEntrypoint, Response

class Default(WorkerEntrypoint):
    async def fetch(self, request):
        # Name of the cookie
        cookie_name = "__uid"

        cookies = SimpleCookie(request.headers["Cookie"] or "")

        if cookie_name in cookies:
            # Respond with cookie value
            return Response(cookies[cookie_name].value)

        return Response("No cookie with name: " + cookie_name)
import { Hono } from 'hono';
import { getCookie } from 'hono/cookie';

const app = new Hono();

app.get('*', (c) => {
  // The name of the cookie
  const COOKIE_NAME = "__uid";

  // Get the specific cookie value using Hono's cookie helper
  const cookieValue = getCookie(c, COOKIE_NAME);

  if (cookieValue) {
    // Respond with the cookie value
    return c.text(cookieValue);
  }

  return c.text("No cookie with name: " + COOKIE_NAME);
});

export default app;

役に立ちましたか?