0

I want to pass authorization headers from js client to the python code during the socketio connection. I am using SocketIo-client v.4 and flask-socketio v.5.

socketio client connection:

socket = io(`${config.apiUrl}/friendship`, {
  autoConnect: true,
  transportOptions: {
    polling: {
      extraHeaders: {"Authorization": "Bearer abc"},
    },
  }
}),

sample server python code:

class FriendshipNamespace(Namespace):
  def on_connect(self):
    print(request.headers)  # no Authorization key
    print(request.headers.get('Authorization'))  # None
    # join_room(self.room)

But I don't know why my backend doesn't receive this extraHeaders. What is the correct way to send the access token to the server?

Tell me if you need some additional info. Would be grateful for any help, thank you.

Yewgen_Dom
  • 66
  • 6

1 Answers1

0

You are passing options in a format that was used in an older version of the socket.io client.

Example from the documentation:

import { io } from "socket.io-client";

const socket = io({
  extraHeaders: {
    "my-custom-header": "1234"
  }
});

See the current documentation for the extraHeaders option.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • 1
    Thanks, it was helpful. I figured out the problem, I was trying to send credentials within a namespace, while it must be done in the first socket connection. – Yewgen_Dom May 03 '22 at 16:49