HTTP headers in Websockets client API

后端 未结 11 2216
情话喂你
情话喂你 2020-11-22 10:41

Looks like it\'s easy to add custom HTTP headers to your websocket client with any HTTP header client which supports this, but I can\'t find how to do it with the JSON API.

11条回答
  •  隐瞒了意图╮
    2020-11-22 11:08

    HTTP Authorization header problem can be addressed with the following:

    var ws = new WebSocket("ws://username:password@example.com/service");
    

    Then, a proper Basic Authorization HTTP header will be set with the provided username and password. If you need Basic Authorization, then you're all set.


    I want to use Bearer however, and I resorted to the following trick: I connect to the server as follows:

    var ws = new WebSocket("ws://my_token@example.com/service");
    

    And when my code at the server side receives Basic Authorization header with non-empty username and empty password, then it interprets the username as a token.

提交回复
热议问题