简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM