question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to specify headers or cookies to send with connection requests from Node.js

See original GitHub issue

I’m connecting to a primus server from another node.js process and I need to attach a session cookie to the initial http request. When connecting from a browser cookies are automatically attached. I can’t seem to find any documentation on how to do this from a non-browser/Node.js client. The transformer I’m using is websockets

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
lpincacommented, Jan 3, 2016

You can use the transport option. See https://github.com/primus/primus#connecting-from-the-browser. For example in your case:

'use strict';

const Primus = require('primus');
const http = require('http');

const server = http.createServer();
const primus = new Primus(server);

primus.authorize((req, done) => {
  console.log(req.headers.cookie);
  done();
});

primus.on('connection', spark => console.log(spark.id + ' connected'));
primus.on('disconnection', spark => console.log(spark.id + ' disconnected'));

server.listen(3000, () => {
  new primus.Socket('http://localhost:3000', {
    transport: {
      headers: { 'Cookie': 'foo=bar' }
    }
  });
});
0reactions
vinnymaccommented, Sep 21, 2017

@lpinca fortunately this is node, and I can just do it manually. For anyone who comes across this. You can add headers to the initial GET request here, and you can add headers for the websocket connection here. Just add a headers object to the options passed into the WebsocketDriver . Thanks for the prompt response pushing me in the right direction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I send cookie using NodeJs request GET module?
After a few hours I've found a solution, instead of : //requesting data request({ url: 'http://localhost/delivery-stats', method: "GET", header: ...
Read more >
How do I create a HTTP Client Request with a cookie - Edureka
I've got a node.js Connect server that checks the request's cookies. ... 'cookie' header for this, but I'm not sure how to set...
Read more >
How to set cookies when send a request in node ? · Issue #943
The cookies need to be passed into the headers object. Axios.request({ url: "http://example.com" ...
Read more >
Understanding Cookies and Implementing them in Node.js
Step 1 - Set a cookie. We will set a route that will save a cookie in the browser. In this case, the...
Read more >
Using HTTP cookies - MDN Web Docs
After receiving an HTTP request, a server can send one or more Set-Cookie headers with the response. The browser usually stores the cookie...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found