[英]How to send requests using custom cookies with Request-promise
我正在開發一個打印應用程序,我需要從受保護的資源中獲取一些數據。 我可以登錄的唯一方法是使用瀏覽器,所以我使用幻像作為瀏覽器。 我設法登錄並獲取 cookie,但我不知道如何使用帶有 request-promise 的 cookie 來發送帶有登錄時獲得的 cookie 的請求。
這是我的簡化代碼:
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 = "test@email.com";
(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")
})();
文檔對此非常不清楚,但cookie
函數實際上只是解析並返回一個 cookie 字符串作為對象。 它不會設置要在請求中發送的 cookie。
你有兩個選擇。 要么使用tough-cookie
來創建cookie jar 並將其包含在選項中,如官方文檔中所示,要么直接將cookie 包含在標題中。
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);
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.