2

I am calling below url with get https request using Node.js, here i need to send the session with cookie, since i know the vallue of cookie.

var URL = require('url-parse');

var appurl = "https://test.somedomain.com"
var https = require('https');
var url = new URL(appurl);
var options = {
    host: url.host,
    url: appurl,
    path: url.pathname + url.query,
    method: 'GET',
    headers: {
        "Set-Cookie": "cookiValue1=abcdesg"
    }
};

var req = https.request(options, function (res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    // console.log(res.headers.)
    // res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ', chunk);
    });
});
req.on('error', function (e) {
    console.log('problem with request: ' + e.message);
});

req.write('data\n');
req.write('data\n');
req.end();
Oxi
  • 2,918
  • 17
  • 28
  • 4
    Possible duplicate of [How to set cookie in node js using express framework?](https://stackoverflow.com/questions/16209145/how-to-set-cookie-in-node-js-using-express-framework) – O. Jones Apr 03 '19 at 11:43
  • here i am asking how to send cookie from client side. – akshata kanabur Apr 04 '19 at 05:34
  • Please [edit] your question to explain your situation a little more clearly. Or, if this question is closed, please ask another. Do you want Javascript running in a browser to set a cookie in that browser? Do you want that Javascript to include a cookie in a request to some server? – O. Jones Apr 04 '19 at 10:29

2 Answers2

0

The header should be Cookie instead of Set-Cookie

Set-Cookie is used by the server to set a cookie on clients. Header Cookie is used to send a cookie to server by client.

Oxi
  • 2,918
  • 17
  • 28
0

So nodejs server it's not a browser which have window and document

try to send the same request in js client side not nodejs

You can set cookie only here

see POINTERs in the bottom

var URL = require('url-parse');
var https = require('https');
var appurl = "https://stackoverflow.com/"
var url = new URL(appurl);
var options = {
    host: url.host,
    url: appurl,
    path: url.pathname + url.query,
    method: 'GET',
    headers: {
        "Cookie": "1111111111=abcdesg", // POINTER its unfortunately useless
        "Set-Cookie": "11111111=abcdesg" // POINTER its unfortunately useless
    }
};

var req = https.request(options, function (res) {
    console.log('before HEADERS: ' + JSON.stringify(res.headers));
    res.headers.cookiValue1 = 'abcdesg'; // POINTER
    console.log('after HEADERS: ' + JSON.stringify(res.headers));

});
req.on('error', function (e) {
    console.log('problem with request: ' + e.message);
});

req.write('data\n');
req.write('data\n');
req.end();
аlex
  • 5,426
  • 1
  • 29
  • 38