6

I want to add basic auth header in the autobahn client tool. How can I achieve that?

Current request

GET / HTTP/1.1  
User-Agent: AutobahnPython/0.5.2  
Host: 10.35.34.172:9000  
Upgrade: WebSocket  
Connection: Upgrade  
Sec-WebSocket-Key: 1U4OeBs60qkmk1V/8voLOw==  
Sec-WebSocket-Version: 8  

Request I need:

GET / HTTP/1.1  
User-Agent: AutobahnPython/0.5.2  
Host: 10.35.34.172:9000  
Authorization: Basic TXlMb2NhdGlvbkFwcDpNeUxvY2F0aW9uQXBwMTIz  
Upgrade: WebSocket  
Connection: Upgrade  
Sec-WebSocket-Key: 1U4OeBs60qkmk1V/8voLOw==  
Sec-WebSocket-Version: 8  

Note: I don't want autobahn server to authenticate the client.
My scenario is autobahn client --> my server --> autobahn server.
My server will take care of extracting the authorization header and then invoke the autobahn server.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
vinbha
  • 91
  • 1
  • 4

1 Answers1

7

I have implemented options to send custom HTTP headers for both AutobahnPython client and server. You need to use the latest AutobahnPython on GitHub (master branch).

A client can send headers by providing a headers keyword argument during construction of the WebSocketClientFactory or set headers via setSessionParameters.

A server can send headers similar to client, and additionally specify headers when returning from onConnect.

Here is a complete example.

Disclaimer: I am original author of Autobahn and work for Tavendo.

Jim Hoagland
  • 481
  • 1
  • 4
  • 20
oberstet
  • 21,353
  • 10
  • 64
  • 97
  • Thanks Oberstet. I have updated my code with master branch. In your client code http://autobahn.ws/python/tutorials/echo (client.py), I have added header as factory = WebSocketClientFactory("ws://192.168.2.138:8081", debug = True, headers = {"Authorization: Basic cmljazpyaWNrMTIz"}) But I am getting error as factory = WebSocketClientFactory("ws://192.168.2.138:8081", debug = True, headers = {"Authorization: Basic cmljazpyaWNrMTIz"}) TypeError: __init__() got an unexpected keyword argument 'headers' Am I doing something wrong? – vinbha Apr 25 '13 at 06:40
  • Please do fetch again the latest code from GitHub, and don't forget to `python setup.py install`. I have also added a complete example: https://github.com/tavendo/AutobahnPython/tree/master/examples/websocket/echo_httpheaders – oberstet Apr 25 '13 at 09:27
  • Do you have an example with autobahn JS client from HTML? – Rafael Freitas Mar 10 '16 at 13:24
  • @RafaelFreitas for autobahn-js you can pass custom headers when initializing a new connection using the `headers` option. ex: `new Autobahn.Connection({ headers: { ... }, ...options, });` – Marian Tarlungeanu Aug 06 '20 at 08:01