graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Remove UTF-8 encoding from SendQueryAsync function

Open eriksylwan opened this issue 5 years ago • 7 comments
trafficstars

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

eriksylwan avatar Jun 01 '20 06:06 eriksylwan

I have the same problem.

.net core 3.0, graphql-client 3.1.2

openclue avatar Jun 01 '20 08:06 openclue

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.

ErikasKontenis avatar Jul 03 '20 15:07 ErikasKontenis

A better workaround would be to override GraphQLHttpRequest.ToHttpRequestMessage, that's where the charset is specified.

rose-a avatar Jul 06 '20 07:07 rose-a

Btw, the GraphQL Spec defines strings as UTF‐8 character sequences...

rose-a avatar Jul 06 '20 07:07 rose-a

Well, GraphQL spec defines string encoding in request text itself but not encoding of transport message.

sungam3r avatar Jul 06 '20 07:07 sungam3r

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'?

uttamukkoji avatar Mar 31 '21 08:03 uttamukkoji

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:

  1. 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;
        }
    }
}
  1. 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);

uttamukkoji avatar Apr 01 '21 12:04 uttamukkoji