From 1c4298727cfa3f6e7c64001cbdfb20e4d326556c Mon Sep 17 00:00:00 2001 From: JasminMistry <4480577+jasmin-mistry@users.noreply.github.com> Date: Tue, 17 Apr 2018 13:18:00 +0100 Subject: [PATCH] fix: removed the utf-8 encoding from the PostAsync fucntion --- src/GraphQL.Client/GraphQLClient.cs | 10 +++++++--- src/GraphQL.Client/GraphQLClientOptions.cs | 2 +- .../GraphQLClientPostTests.cs | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/GraphQL.Client/GraphQLClient.cs b/src/GraphQL.Client/GraphQLClient.cs index 543da712..1985dc6e 100644 --- a/src/GraphQL.Client/GraphQLClient.cs +++ b/src/GraphQL.Client/GraphQLClient.cs @@ -177,9 +177,13 @@ public async Task PostAsync(GraphQLRequest request, Cancellatio if (request.Query == null) { throw new ArgumentNullException(nameof(request.Query)); } var graphQLString = JsonConvert.SerializeObject(request, this.Options.JsonSerializerSettings); - using (var httpContent = new StringContent(graphQLString, Encoding.UTF8, this.Options.MediaType.MediaType)) - using (var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent, cancellationToken).ConfigureAwait(false)) { - return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false); + using (var httpContent = new StringContent(graphQLString)) + { + httpContent.Headers.ContentType = this.Options.MediaType; + using (var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent, cancellationToken).ConfigureAwait(false)) + { + return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false); + } } } diff --git a/src/GraphQL.Client/GraphQLClientOptions.cs b/src/GraphQL.Client/GraphQLClientOptions.cs index ce2e4c4d..ab7fd8d7 100644 --- a/src/GraphQL.Client/GraphQLClientOptions.cs +++ b/src/GraphQL.Client/GraphQLClientOptions.cs @@ -31,7 +31,7 @@ public class GraphQLClientOptions { /// /// The that will be send on POST /// - public MediaTypeHeaderValue MediaType { get; set; } = new MediaTypeHeaderValue("application/json"); // This should be "application/graphql" also "application/x-www-form-urlencoded" is Accepted + public MediaTypeHeaderValue MediaType { get; set; } = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // This should be "application/graphql" also "application/x-www-form-urlencoded" is Accepted } diff --git a/tests/GraphQL.Client.Tests/GraphQLClientPostTests.cs b/tests/GraphQL.Client.Tests/GraphQLClientPostTests.cs index 5898d40d..ba56c9a1 100644 --- a/tests/GraphQL.Client.Tests/GraphQLClientPostTests.cs +++ b/tests/GraphQL.Client.Tests/GraphQLClientPostTests.cs @@ -1,3 +1,4 @@ +using System.Net.Http.Headers; using GraphQL.Common.Request; using GraphQL.Common.Tests.Model; using Xunit; @@ -22,6 +23,25 @@ public async void QueryPostAsyncFact() { Assert.Equal("Luke Skywalker", response.GetDataFieldAs("person").Name); } + [Fact] + public async void QueryPostAsyncWithoutUtf8EncodingFact() + { + var graphQLRequest = new GraphQLRequest + { + Query = @" + { + person(personID: ""1"") { + name + } + }" + }; + this.GraphQLClient.Options.MediaType = MediaTypeHeaderValue.Parse("application/json"); + var response = await this.GraphQLClient.PostAsync(graphQLRequest).ConfigureAwait(false); + + Assert.Equal("Luke Skywalker", response.Data.person.name.Value); + Assert.Equal("Luke Skywalker", response.GetDataFieldAs("person").Name); + } + [Fact] public async void OperationNamePostAsyncFact() { var graphQLRequest = new GraphQLRequest {