reactjs - How can I set cookie header using react and request-promise
问题描述
"Cookie" header is never sent, even when browser console shows otherwise
I am trying to use request-promise and Reactjs to send request from my web application to SAP B1 Service Layer. First, I tried using the "headers" option in request-promise, then tried setting a cookie using rp.cookie("...") and jar.setCookie("...") but got errors both times. The browser console is showing it is sending the "Cookie" header, but when I intercept the request using Burp Suite the header is never sent.
var rp = require('request-promise').defaults({jar:true});
var rd= require('request-debug');
rd(rp);
const b1jar=rp.jar();
let options = {
method: method,
rejectUnauthorized: false,
uri: url,
insecure:true,
jar:b1jar,
json: true,
resolveWithFullResponse:fullres,
// I TRIED TO USE THIS FIRST
headers:
{ 'Cookie': "B1SESSION="+(sessionStorage.SessionID||'')+';',
}
};
//THEN DELETED THE headers OPTION AND TRIED WITH THE jar OPTION
var b1cookie=rp.cookie('B1SESSION='+sessionStorage.SessionID);
b1cookie.setExpires(moment().add(sessionStorage.SessionTimeout,'m')
.toDate());
b1jar.setCookie(b1cookie,config.server);
res = await rp(options);
The expected result is that the B1SESSION Cookie should be sent when using rp(options), but it is not.
解决方案
那个API很糟糕,试试axios ...
https://www.npmjs.com/package/react-axios
推荐阅读
- sql - 在SQL中累积当前行和下一行的总和
- firefox - 为 Chrome、Firefox 和 Edge 等 Web 浏览器自定义鼠标右键单击弹出菜单
- azure - 如何在 Azure 门户中以最新的优先顺序列出 blob 文件
- windows - 如何在上下文菜单中使用 Imagemagick 将图像合并为 PDF
- machine-learning - ValueError: X 有 10 个特征,但 DecisionTreeClassifier 期望 11 个特征作为输入
- kubernetes - GCP Firestore:服务器请求失败,GKE 权限缺失或权限不足
- perl - 为什么我得到三个用户输出而不是 2
- html - 如何使 id 的 href 看起来像一个完全不同的页面(单击时)?
- javascript - 当酱文件位于 node_modules 中时,我该如何在我的网站上使用 npm js 包?
- powershell - 如何在命令提示符窗口中将 powershell 多行命令用作单行