1

We have to implement WebSocket connection with third-party WebSocket server. But they are using Authorization header for establishing a connection between WebSocket client and WebSocket server. But I could not find any solution to add Authorization header in WebSocket client during connection. Please help me to resolve this issue.

This is my code I am using

var ws = new WebSocketClient();
var headers ={Authorization: 'token'}
ws.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});

ws.on('connect', function(connection) {
    console.log('WebSocket Client Connected');
});
ws.connect('wss://url', '',headers);

Response is like this

 Server responded with a non-101 status: 400 Bad Request
Response Headers Follow:
server: nginx/1.11.7
date: Thu, 11 Jul 2019 19:49:58 GMT
content-type: text/html
content-length: 31
connection: keep-alive
anas p a
  • 383
  • 5
  • 20
  • What's the Web Socket client? Your Node.js server? And if so, what Web Socket client are you using? – Brad Jul 11 '19 at 18:55
  • We are using normal WebSocket client (WebSocket). We don't know about the server it is a third-party one – anas p a Jul 11 '19 at 19:09
  • There isn't some "normal" WebSocket client, there are a handful commonly used. You should be able to look at your code and package.json and figure out which it is. Is it this? https://www.npmjs.com/package/websocket – Brad Jul 11 '19 at 19:21
  • yes it is the one – anas p a Jul 11 '19 at 19:26
  • Right in the documentation is an example for passing additional headers: https://github.com/theturtle32/WebSocket-Node/blob/6e62787df0ccc9a34b12c134686aef07716849f3/docs/WebSocketClient.md#connectrequesturl-requestedprotocols-origin-headers-requestoptions – Brad Jul 11 '19 at 19:29
  • I tried but I got 400 bad request error Is any way see the request header values in this library – anas p a Jul 11 '19 at 19:31
  • Why don't you post your code then? If you've already been able to add this header, then you already know how to do it, which is what your question is asking. It's really frustrating to try to help someone without the extra context. That's why on Stack Overflow we ask for examples and ask you to better explain what you've tried. – Brad Jul 11 '19 at 19:32
  • @Brad sorry for my mistake. I was not sure I am in right way. That's why asked like this. Sorry again. Updated the question with my code and response – anas p a Jul 11 '19 at 19:54
  • It doesn't look like `headers` are actually used. – Brad Jul 11 '19 at 19:55
  • It was actually I removed the header's to whether what happening when its is not there Updated the question with using headers – anas p a Jul 11 '19 at 19:58
  • 1
    Ok, and did you see this part of the documentation? "origin must be specified if you want to pass headers, and both origin and headers must be specified if you want to pass requestOptions. The origin and headers parameters may be passed as null." You're not passing an origin. – Brad Jul 11 '19 at 20:00
  • Can we put * as origin? – anas p a Jul 11 '19 at 20:02
  • That's up to the server you're connecting to. – Brad Jul 11 '19 at 20:03
  • ok, Do we have any option to see the request header of request? – anas p a Jul 11 '19 at 20:05
  • Thanks, @Brad, I could able to connect to that server. – anas p a Jul 22 '19 at 06:32
  • Does this answer your question? [HTTP headers in Websockets client API](https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api) – Nicolas Martinez Jul 14 '20 at 18:15

0 Answers0