What is the correct method / syntax for adding headers to a websocket connection request using Python Websockets ?
The server I'm trying to connect to requires headers in the connection request for authentication
async def connect():
async with websockets.connect("wss://site.com/ws") as websocket:
response = await websocket.recv()
print(response)
# eg. does not work:
async with websockets.connect(f"wss://site.com/ws, header={headers}") as websocket:
async with websockets.connect(f"wss://site.com/ws, extra_headers:{headers}") as websocket:
Similar question was asked here but did not answer the core of the question: How to send 'Headers' in websocket python
According to documentation, you can pass headers in extra_headers
param of connect()
function. Details here.
So code should look something like this:
async def connect():
async with websockets.connect("wss://site.com/ws", extra_headers=headers) as websocket:
response = await websocket.recv()
print(response)