From d65ff138954bc6e0fdf6fc28b4271910ca77eacd Mon Sep 17 00:00:00 2001 From: analog-nico Date: Sun, 7 May 2017 18:20:30 -0700 Subject: [PATCH] docs: example of cookie handling fixes #79 --- README.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e83feb8..c831a71 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,7 @@ var options = { some: 'payload' // Will be urlencoded }, headers: { - /* 'content-type': 'application/x-www-form-urlencoded' */ // Set automatically + /* 'content-type': 'application/x-www-form-urlencoded' */ // Is set automatically } }; @@ -159,6 +159,39 @@ rp(options) }); ``` +### Include a cookie + +``` js +var tough = require('tough-cookie'); + +// Easy creation of the cookie - see tough-cookie docs for details +let cookie = new tough.Cookie({ + key: "some_key", + value: "some_value", + domain: 'api.mydomain.com', + httpOnly: true, + maxAge: 31536000 +}); + +// Put cookie in an jar which can be used across multiple requests +var cookiejar = rp.jar(); +cookiejar.setCookie(cookie, 'https://api.mydomain.com'); +// ...all requests to https://api.mydomain.com will include the cookie + +var options = { + uri: 'https://api.mydomain.com/...', + jar: cookiejar // Tells rp to include cookies in jar that match uri +}; + +rp(options) + .then(function (body) { + // Request succeeded... + }) + .catch(function (err) { + // Request failed... + }); +``` + ### Get the full response instead of just the body ``` js