magic-cookie
v0.0.5
Published
Magic Cookie
Downloads
4
Readme
Magic Cookie
Supports Redis >= 2.6.12 and (Node.js >= 6).
Description
Another redis store for tough-cookie module.
Install
$ npm install magic-cookie
Basic Usage
const rp = require('request-promise');
const { MemoryCookieStore } = require('magic-cookie');
const jar = rp.jar(new MemoryCookieStore());
(async () => {
const qs = {
key: 'value',
anotherkey: 'something',
someelse: 'content',
};
const response = await rp('https://httpbin.org/cookies/set', { qs, jar, json: true });
console.log(response);
})();
Import cookie from Puppeteer to Request
Use the loadPuppeteerCookie() method to import a Puppeteer cookie.
const rp = require('request-promise');
const puppeteer = require('puppeteer');
const querystring = require('querystring');
const { MemoryCookieStore } = require('./');
(async () => {
const qs = {
key: 'value',
anotherkey: 'something',
someelse: 'content',
};
const urlSet = `https://httpbin.org/cookies/set?${querystring.stringify(qs)}`;
const urlGet = 'https://httpbin.org/cookies';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(urlSet, { waitUntil: 'networkidle2' });
const cookies = await page.cookies();
await browser.close();
const cookieMemory = new MemoryCookieStore();
cookieMemory.loadPuppeteerCookie(cookies);
const jar = rp.jar(cookieMemory);
const response = await rp(urlGet, { jar, json: true });
console.log(response);
})();
Import cookie from Request to Puppeteer
Use the loadPuppeteerCookie() method to import a Puppeteer cookie.
const rp = require('request-promise');
const puppeteer = require('puppeteer');
const { MemoryCookieStore } = require('magic-cookie');
(async () => {
const qs = {
key: 'value',
anotherkey: 'something',
someelse: 'content',
};
const urlSet = 'https://httpbin.org/cookies/set';
const urlGet = 'https://httpbin.org/cookies';
const cookieMemory = new MemoryCookieStore();
const jar = rp.jar(cookieMemory);
await rp(urlSet, { qs, jar, json: true });
const cookies = cookieMemory.getPuppeteerCookie();
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setCookie(...cookies);
await page.goto(urlGet, { waitUntil: 'networkidle2' });
const bodyHandle = await page.$('body');
const response = await page.evaluate(body => JSON.parse(body.innerText), bodyHandle);
await browser.close();
console.log(response);
})();
Options to use with Redis Store
path
optional You can specify which Redis address to connect. [default: 'localhost:6379 db 0']id
optional ID for each redis store so that we can use multiple stores with the same redis database [default: 'default']
const rp = require('request-promise');
const { RedisStore } = require('magic-cookie');
// Connect to 127.0.0.1:6380, db 4, using password "authpassword" and stores on key "my-cookie"
const jar = rp.jar(new RedisStore('redis://:[email protected]:6380/4', 'my-cookie'));
(async () => {
const qs = {
key: 'value',
anotherkey: 'something',
someelse: 'content',
};
const response = await rp('https://httpbin.org/cookies/set', { qs, jar, json: true });
console.log(response);
})();