From ce101a0f8632f6b5390af247f3df2002e86becdf Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Sun, 28 Apr 2019 01:07:11 +0200 Subject: http: Cookie improvements (denoland/deno_std#359) Original: https://github.com/denoland/deno_std/commit/f1114691038888fc3d8995b64a8028f072569672 --- http/README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'http/README.md') diff --git a/http/README.md b/http/README.md index 448692a68..c7bac3f09 100644 --- a/http/README.md +++ b/http/README.md @@ -2,6 +2,45 @@ A framework for creating HTTP/HTTPS server. +## Cookie + +Helper to manipulate `Cookie` throught `ServerRequest` and `Response`. + +```ts +import { getCookies } from "https://deno.land/std/http/cookie.ts"; + +let req = new ServerRequest(); +req.headers = new Headers(); +req.headers.set("Cookie", "full=of; tasty=chocolate"); + +const c = getCookies(request); +// c = { full: "of", tasty: "chocolate" } +``` + +To set a `Cookie` you can add `CookieOptions` to properly set your `Cookie` + +```ts +import { setCookie } from "https://deno.land/std/http/cookie.ts"; + +let res: Response = {}; +res.headers = new Headers(); +setCookie(res, { name: "Space", value: "Cat" }); +``` + +Deleting a `Cookie` will set its expiration date before now. +Forcing the browser to delete it. + +```ts +import { delCookie } from "https://deno.land/std/http/cookie.ts"; + +let res = new Response(); +delCookie(res, "deno"); +// Will append this header in the response +// "Set-Cookie: deno=; Expires=Thus, 01 Jan 1970 00:00:00 GMT" +``` + +**Note**: At the moment multiple `Set-Cookie` in a `Response` is not handled. + ## Example ```typescript -- cgit v1.2.3