Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/GraphQL.Client/GraphQLClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,13 @@ public async Task<GraphQLResponse> 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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.Client/GraphQLClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class GraphQLClientOptions {
/// <summary>
/// The <see cref="MediaTypeHeaderValue"/> that will be send on POST
/// </summary>
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

}

Expand Down
20 changes: 20 additions & 0 deletions tests/GraphQL.Client.Tests/GraphQLClientPostTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net.Http.Headers;
using GraphQL.Common.Request;
using GraphQL.Common.Tests.Model;
using Xunit;
Expand All @@ -22,6 +23,25 @@ public async void QueryPostAsyncFact() {
Assert.Equal("Luke Skywalker", response.GetDataFieldAs<Person>("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>("person").Name);
}

[Fact]
public async void OperationNamePostAsyncFact() {
var graphQLRequest = new GraphQLRequest {
Expand Down