Ad

How To Send Requests Using Custom Cookies With Request-promise

- 1 answer

I am working on a print app and I need to get some data from a protected resources. the only way I can be signed in is using a browser so I am using phantom as browser. I manged to sign in and get the cookies but I don't know how to use the cookies with request-promise to send the requests with the cookies I got when I signed in.

Here is my simplified code:

import * as phantom from "phantom";
import * as requestPromise from "request-promise-native";
requestPromise.defaults({ rejectUnauthorized: false });

(async function () {
    const instance = await phantom.create([
        "--ignore-ssl-errors=true",
        "--cookies-file=cookies.txt",
        "--web-security=false",
        "--ssl-protocol=any"
    ]);
    const page = await instance.createPage();
    const status = await page.open("http://localhost:3000/auth/login-html");
    console.log("status", status);

    let result = await page.evaluate(() => {
        (document.getElementById("username") as HTMLInputElement).value = "[email protected]";
        (document.getElementById("password") as HTMLInputElement).value="password";
        (document.getElementById("login") as HTMLElement).click();

        return true;
    });
    // Get the cookies 

    let cookies = await page.property("cookies");
    console.log(cookies)
    /** [{ domain: 'my-app-resource-domain.com',
        httponly: true,
        name: 'ticket',
        path: '/',
        secure: false,
        value: '6823a-78c0a4ee82c0' },
        { domain: 'my-app-resource-domain.com',
        httponly: true,
        name: 'JSESSIONID',
        path: '/',
        secure: false,
        value: '9CB8396A05ABA178' }] **/
    let cookiesSring = getStringCookies(cookies) // "ticket=6823a-78c0a4ee82c0&JSESSIONID=9CB8396A05ABA178"
    requestPromise.cookie(cookiesSring );

    // Send request to get data from protected resource 
    let res = await requestPromise.get("http://my-app-resource-domain.com/api/get-charts")
})();
Ad

Answer

The documentation is very unclear about it, but the cookie function actually only just parses and returns a cookie string as an object. It does not set the cookie to be sent in the request.

You have two options. Either you use tough-cookie to create a cookie jar and include it in the options, as shown in the official documentation, or you simply include your cookies in the header directly.

Using a cookie jar

import * as requestPromise from "request-promise-native";
import { Cookie } from "tough-cookie";

// ...

const pageCookies = await page.property("cookies");
// rename the "httponly" property to "httpOnly" since that's what
// tough-cookie expects
const cookies = pageCookies.map(pageCookie => {
    const cookie = { ...pageCookie };
    cookie.httpOnly = pageCookie.httponly;
    delete cookie.httpOnly;
    return new Cookie(cookie);
});

const cookieJar = requestPromise.jar();
// Set the cookies to apply only to my-app-resource-domain.com
cookies.forEach(cookie =>
    cookieJar.setCookie(cookie, "http://my-app-resource-domain.com/")
);

const options = {
    uri: "http://my-app-resource-domain.com/api/get-charts",
    jar: cookieJar
};

const res = await requestPromise.get(options);

Using headers

import * as requestPromise from "request-promise-native";
import * as querystring from "querystring";

// ...

const pageCookies = await page.property("cookies");

// pluck the name and value from the page cookies into
// key-value pairs in an object
const cookieObjects = pageCookies.reduce((acc, cookie) => {
    acc[cookie.name] = cookie.value;
    return acc;
}, {});

// stringify the cookies to the familiar "cookie=value; cookie2=value" format
const cookieString = querystring.stringify(cookieObjects, "; ");

const options = {
    uri: "http://my-app-resource-domain.com/api/get-charts",
    headers: {
        Cookie: `${cookieString};`
    }
};

const res = await requestPromise.get(options);
Ad
source: stackoverflow.com
Ad