From cd8f238f835d7f095a12a63bcb2aa45ee0e0775e Mon Sep 17 00:00:00 2001 From: Brian Mortensen Date: Wed, 16 May 2018 15:31:39 -0700 Subject: [PATCH] Add Content-Type header in HttpClient.ts (#2242) --- clients/ts/FunctionalTests/TestHub.cs | 5 +++++ .../FunctionalTests/ts/HubConnectionTests.ts | 18 ++++++++++++++++++ clients/ts/signalr/src/HttpClient.ts | 2 ++ 3 files changed, 25 insertions(+) diff --git a/clients/ts/FunctionalTests/TestHub.cs b/clients/ts/FunctionalTests/TestHub.cs index 6c4275ee91..53230b5270 100644 --- a/clients/ts/FunctionalTests/TestHub.cs +++ b/clients/ts/FunctionalTests/TestHub.cs @@ -83,5 +83,10 @@ namespace FunctionalTests String = "hello world", }; } + + public string GetContentTypeHeader() + { + return Context.GetHttpContext().Request.Headers["Content-Type"]; + } } } diff --git a/clients/ts/FunctionalTests/ts/HubConnectionTests.ts b/clients/ts/FunctionalTests/ts/HubConnectionTests.ts index b1ad104481..6a4a84e853 100644 --- a/clients/ts/FunctionalTests/ts/HubConnectionTests.ts +++ b/clients/ts/FunctionalTests/ts/HubConnectionTests.ts @@ -686,6 +686,24 @@ describe("hubConnection", () => { } }); + it("populates the Content-Type header when sending XMLHttpRequest", async (done) => { + const hubConnection = getConnectionBuilder(HttpTransportType.LongPolling, TESTHUB_NOWEBSOCKETS_ENDPOINT_URL) + .withHubProtocol(new JsonHubProtocol()) + .build(); + + try { + await hubConnection.start(); + + // Check what transport was used by asking the server to tell us. + expect(await hubConnection.invoke("GetActiveTransportName")).toEqual("LongPolling"); + // Check to see that the Content-Type header is set the expected value + expect(await hubConnection.invoke("GetContentTypeHeader")).toEqual("text/plain;charset=UTF-8"); + done(); + } catch (e) { + fail(e); + } + }); + function getJwtToken(url: string): Promise { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); diff --git a/clients/ts/signalr/src/HttpClient.ts b/clients/ts/signalr/src/HttpClient.ts index 66837a01a0..7fb94fac0a 100644 --- a/clients/ts/signalr/src/HttpClient.ts +++ b/clients/ts/signalr/src/HttpClient.ts @@ -163,6 +163,8 @@ export class DefaultHttpClient extends HttpClient { xhr.open(request.method, request.url, true); xhr.withCredentials = true; xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + // Explicitly setting the Content-Type header for React Native on Android platform. + xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); if (request.headers) { Object.keys(request.headers)