HTTP headers in Websockets client API

后端 未结 11 2419
情话喂你
情话喂你 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 11:07

    In my situation (Azure Time Series Insights wss://)

    Using the ReconnectingWebsocket wrapper and was able to achieve adding headers with a simple solution:

    socket.onopen = function(e) {
        socket.send(payload);
    };
    

    Where payload in this case is:

    {
      "headers": {
        "Authorization": "Bearer TOKEN",
        "x-ms-client-request-id": "CLIENT_ID"
    }, 
    "content": {
      "searchSpan": {
        "from": "UTCDATETIME",
        "to": "UTCDATETIME"
      },
    "top": {
      "sort": [
        {
          "input": {"builtInProperty": "$ts"},
          "order": "Asc"
        }], 
    "count": 1000
    }}}
    

提交回复
热议问题