Skip to content

Commit 8a406a6

Browse files
jasmin-mistryRaul Hidalgo Caballero
authored andcommitted
fix: removed the utf-8 encoding from the PostAsync fucntion (#37)
1 parent 210b3e1 commit 8a406a6

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/GraphQL.Client/GraphQLClient.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ public async Task<GraphQLResponse> PostAsync(GraphQLRequest request, Cancellatio
177177
if (request.Query == null) { throw new ArgumentNullException(nameof(request.Query)); }
178178

179179
var graphQLString = JsonConvert.SerializeObject(request, this.Options.JsonSerializerSettings);
180-
using (var httpContent = new StringContent(graphQLString, Encoding.UTF8, this.Options.MediaType.MediaType))
181-
using (var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent, cancellationToken).ConfigureAwait(false)) {
182-
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
180+
using (var httpContent = new StringContent(graphQLString))
181+
{
182+
httpContent.Headers.ContentType = this.Options.MediaType;
183+
using (var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent, cancellationToken).ConfigureAwait(false))
184+
{
185+
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
186+
}
183187
}
184188
}
185189

src/GraphQL.Client/GraphQLClientOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class GraphQLClientOptions {
3131
/// <summary>
3232
/// The <see cref="MediaTypeHeaderValue"/> that will be send on POST
3333
/// </summary>
34-
public MediaTypeHeaderValue MediaType { get; set; } = new MediaTypeHeaderValue("application/json"); // This should be "application/graphql" also "application/x-www-form-urlencoded" is Accepted
34+
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
3535

3636
}
3737

tests/GraphQL.Client.Tests/GraphQLClientPostTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net.Http.Headers;
12
using GraphQL.Common.Request;
23
using GraphQL.Common.Tests.Model;
34
using Xunit;
@@ -22,6 +23,25 @@ public async void QueryPostAsyncFact() {
2223
Assert.Equal("Luke Skywalker", response.GetDataFieldAs<Person>("person").Name);
2324
}
2425

26+
[Fact]
27+
public async void QueryPostAsyncWithoutUtf8EncodingFact()
28+
{
29+
var graphQLRequest = new GraphQLRequest
30+
{
31+
Query = @"
32+
{
33+
person(personID: ""1"") {
34+
name
35+
}
36+
}"
37+
};
38+
this.GraphQLClient.Options.MediaType = MediaTypeHeaderValue.Parse("application/json");
39+
var response = await this.GraphQLClient.PostAsync(graphQLRequest).ConfigureAwait(false);
40+
41+
Assert.Equal("Luke Skywalker", response.Data.person.name.Value);
42+
Assert.Equal("Luke Skywalker", response.GetDataFieldAs<Person>("person").Name);
43+
}
44+
2545
[Fact]
2646
public async void OperationNamePostAsyncFact() {
2747
var graphQLRequest = new GraphQLRequest {

0 commit comments

Comments
 (0)