graphql-client
graphql-client copied to clipboard
Remove UTF-8 encoding from SendQueryAsync function
How can I remove the charset=utf-8 from the request header when using the SendQueryAsync?
I have tried setting the GraphQLHttpClientOptions.MediaType to "application/json" (wich I also think is the default, so it shouldn't be necessary), but I still keep getting the Content-type header of the request to : application/json; charset=utf-8, which is rejected by the service I'm requesting, it needs to be just application/json.
My project is a .net core 3.1 project and the graphql-client is v3.1.2
I have the same problem.
.net core 3.0, graphql-client 3.1.2
Hi, guys same issue here with .net core 2.2, graphql-client 3.1.3. Could this charset=utf-8 be configurable with ability to simply remove? Cheers. As a workaround I have rollbacked the nuget to lower version where the utf-8 param wasn't implemented.
A better workaround would be to override GraphQLHttpRequest.ToHttpRequestMessage, that's where the charset is specified.
Btw, the GraphQL Spec defines strings as UTF‐8 character sequences...
Well, GraphQL spec defines string encoding in request text itself but not encoding of transport message.
Hi,
I am facing same issue with the graphql-client v3.1.3. When we can expect the Content-Type configurable which allow to remove 'charset=utf-8'?
Hey,
As @rose-a suggested to override GraphQLHttpRequest.ToHttpRequestMessage method that will remove UTF-8 encoding from Content-Type header. Here is solution that may be helpful for users facing similar issue. Follow the Steps:
- Add following code into your project with file name
CSGraphQLHttpRequest:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using GraphQL;
using GraphQL.Client.Abstractions;
using GraphQL.Client.Http;
namespace Your.Name.Space
{
public class CSGraphQLHttpRequest: GraphQLHttpRequest
{
public CSGraphQLHttpRequest(GraphQLRequest other) : base(other)
{
}
public override HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions options, IGraphQLJsonSerializer serializer)
{
var message = new HttpRequestMessage(HttpMethod.Post, options.EndPoint)
{
Content = new StringContent(serializer.SerializeToString(this), Encoding.UTF8, options.MediaType)
};
message.Content.Headers.ContentType = new MediaTypeHeaderValue(options.MediaType);
#pragma warning disable CS0618 // Type or member is obsolete
PreprocessHttpRequestMessage(message);
#pragma warning restore CS0618 // Type or member is obsolete
return message;
}
}
}
- Update initialization code for GraphQLHttpClient to following:
var graphQLClient = new GraphQLHttpClient("https://api.example.com/graphql", new NewtonsoftJsonSerializer());
//Include following line while initialising
graphQLClient.Options.PreprocessRequest = (request, client) => Task.FromResult(new CSGraphQLHttpRequest(request) as GraphQLHttpRequest);