signalr_client icon indicating copy to clipboard operation
signalr_client copied to clipboard

HubConnectionBuilder not passing Message headers to Server Hub

Open clmorales opened this issue 2 years ago • 16 comments

When configuring HubConnectionBuilder().withUrl options headers. The headers are not been pass to the server hub.

My Code:

final headers = MessageHeaders(); headers.setHeaderValue("MyHeader-One", "MY-VALUE1"); headers.setHeaderValue("MyHeader-Two", "MY-VALUE2");

hubConnection = HubConnectionBuilder() .withUrl( serverUrl, options: HttpConnectionOptions( headers: headers, transport: HttpTransportType.LongPolling, ), ) .withAutomaticReconnect() .build();

Header MyHeader-One and MyHeader-Two are not been received at Hub

clmorales avatar Apr 21 '22 07:04 clmorales

Team any workaround or fix for this?

ranjanraviraj avatar Jun 07 '22 05:06 ranjanraviraj

I have a similar problem, were you able to solve it?

ostup17 avatar Aug 02 '22 09:08 ostup17

No, and end up using signalr_pure, you may try it. Hope it works.

clmorales avatar Aug 02 '22 10:08 clmorales

Can't find headers, how to send them?

ostup17 avatar Aug 02 '22 10:08 ostup17

Can't find headers, how to send them?

Remember this is for the signalr_pure package

  1. Create a HttpConnetionOptions
  2. Set options.headers
  3. Include ...httpConnectionOptions in HubConnectionBuilder

void main() async { final options = HttpConnectionOptions(); options.headers = {'customHeader1': 'value1', 'customHeader2': 'value2'} ; final builder = HubConnectionBuilder() ..url = 'url' ..logLevel = LogLevel.information ..httpConnectionOptions = options ..reconnect = true; final connection = builder.build(); connection.on('send', (args) => print(args)); await connection.startAsync(); await connection.sendAsync('send', ['Hello', 123]); final obj = await connection.invokeAsync('send', ['Hello', 'World']); print(obj); }

clmorales avatar Aug 02 '22 12:08 clmorales

Bro, thanks a lot. I spent about 4 days sending headers via signalr_netcore

ostup17 avatar Aug 03 '22 03:08 ostup17

Here is being created empty MessageHeaders() object instead of using user provided options headers.

leoshusar avatar Aug 14 '22 23:08 leoshusar

Is anyone working on this?

waadsulaiman avatar Sep 19 '22 21:09 waadsulaiman

@clmorales

package: cure: ^0.1.0-nullsafety.0

    final HubConnectionBuilder builder = HubConnectionBuilder()
      ..url = _url
      ..logLevel = LogLevel.critical
      ..httpConnectionOptions = HttpConnectionOptions(
        headers: {
          "remote-ip-address": ipAddress ?? "",
          "platform-type": PlatformInfo().platform.name.capitalizeFirstLetter()
        },
      )
      ..reconnect = true;

      _connection = builder.build();

waadsulaiman avatar Sep 19 '22 22:09 waadsulaiman

Any update on this?

NicolasDionB avatar Feb 13 '23 21:02 NicolasDionB

Really too bad

eifachmache avatar Mar 24 '23 10:03 eifachmache

Is there a solution? I'm trying to send the header but it just won't, I've tried many ways and none of them get the server-side header

JonMuc avatar Aug 30 '23 00:08 JonMuc

The header reaches the server, but it is not recognized during the update process.

Does "X-Requested-With: FlutterHttpClient" not recognize this part?

cyberprophet avatar Nov 12 '23 08:11 cyberprophet

info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase:
      Path: /hubs/intro/negotiate
      Host: 192.168.0.2:19456
      User-Agent: Dart/3.1 (dart:io)
      Accept-Encoding: gzip
      Content-Type: text/plain;charset=UTF-8
      Content-Length: 0
      X-Requested-With: FlutterHttpClient
      useridentifier: [Redacted]

The header reaches the server, but it is not recognized during the update process.

Does X-Requested-With: FlutterHttpClient not recognize this part?

info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: GET
      Scheme: http
      PathBase:
      Path: /hubs/intro
      Connection: Upgrade
      Host: 192.168.0.2:19456
      User-Agent: Dart/3.1 (dart:io)
      Accept-Encoding: gzip
      Cache-Control: no-cache
      Upgrade: websocket
      Sec-WebSocket-Version: 13
      Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
      Sec-WebSocket-Key: [Redacted]

cyberprophet avatar Nov 12 '23 08:11 cyberprophet

WebSocket protocol differs from HTTP/HTTPS by enabling bidirectional data exchange across multiple frames instead of just a single request. A WebSocket connection usually starts with HTTP/HTTPS and then transitions to WebSocket with an upgrade request.

As seen in the provided logs, the indication (Upgrade: websocket) shows that the connection has been upgraded to WebSocket. Subsequently, communication takes place using the WebSocket protocol, where each message is not perceived as a separate HTTP/HTTPS request. Therefore, attempting to find "UserIdentifier" in context.Request.Headers is meaningless.

In WebSocket protocol, a different approach is needed to send custom headers. Typically, custom data is transmitted during connection setup or when sending messages in WebSocket. To include user identification information when setting up a WebSocket connection, you need to add this information on the client side during WebSocket connection establishment. The server can then read and process this information upon WebSocket connection establishment. The specific method may vary depending on the WebSocket library and framework in use. If you can provide additional information about the WebSocket library you are using, it would be helpful.

cyberprophet avatar Nov 12 '23 09:11 cyberprophet

Can't find headers, how to send them?

Remember this is for the signalr_pure package

  1. Create a HttpConnetionOptions
  2. Set options.headers
  3. Include ...httpConnectionOptions in HubConnectionBuilder

void main() async { final options = HttpConnectionOptions(); options.headers = {'customHeader1': 'value1', 'customHeader2': 'value2'} ; final builder = HubConnectionBuilder() ..url = 'url' ..logLevel = LogLevel.information ..httpConnectionOptions = options ..reconnect = true; final connection = builder.build(); connection.on('send', (args) => print(args)); await connection.startAsync(); await connection.sendAsync('send', ['Hello', 123]); final obj = await connection.invokeAsync('send', ['Hello', 'World']); print(obj); }

Has the header transmission been resolved?

cyberprophet avatar Nov 12 '23 09:11 cyberprophet