Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix http README examples #440

Merged
merged 1 commit into from
May 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,46 @@ A framework for creating HTTP/HTTPS server.
Helper to manipulate `Cookie` throught `ServerRequest` and `Response`.

```ts
import { ServerRequest } from "https://deno.land/std/http/server.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");
let request = new ServerRequest();
request.headers = new Headers();
request.headers.set("Cookie", "full=of; tasty=chocolate");

const c = getCookies(request);
// c = { full: "of", tasty: "chocolate" }
const cookies = getCookies(request);
console.log("cookies:", cookies);
// cookies: { 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";
import { Response } from "https://deno.land/std/http/server.ts";
import { Cookie, setCookie } from "https://deno.land/std/http/cookie.ts";

let res: Response = {};
res.headers = new Headers();
setCookie(res, { name: "Space", value: "Cat" });
let response: Response = {};
const cookie: Cookie = { name: "Space", value: "Cat" };
setCookie(response, cookie);

const cookieHeader = response.headers.get("set-cookie");
console.log("Set-Cookie:", cookieHeader);
// Set-Cookie: Space=Cat
```

Deleting a `Cookie` will set its expiration date before now.
Forcing the browser to delete it.

```ts
import { Response } from "https://deno.land/std/http/server.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"
let response: Response = {};
delCookie(response, "deno");

const cookieHeader = response.headers.get("set-cookie");
console.log("Set-Cookie:", cookieHeader);
// 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.
Expand Down