0

I use ASP.NET Core to consume GraphQL API and everything working fine, just I need pass the header because I need to use Arabica language and I don't know how I can pass it.

So I need to change language to "ar" when header is passing.

[HttpPost]
        public async Task<IActionResult> Tracking(string code)
        {
            var options = new GraphQLHttpClientOptions
            {
                EndPoint = new Uri("https://EndPoint/graphql"),
                
                
                // Add any additional configuration options here
            };
            

            var client = new GraphQLHttpClient(options, new NewtonsoftJsonSerializer());
            var query = @"
          query {
             shipment(code: """ + code + @""") {
             id
             code
             customerDue
             createdAt
             collected
             paidToCustomer
             paidToDeliveryAgent
        }
    }";

            var request = new GraphQLRequest
            {
                Query = query
            };

            var response = await client.SendQueryAsync<dynamic>(request);

           
            if (response.Errors != null) {

                foreach (var error in response.Errors)
                {
                    Console.WriteLine($"GraphQL Error: {error.Message}");
                }
                return NotFound();

            }
            else
            {
                var data = response.Data;
                var shipment = new Shipment
                {

                    id = data.shipment.id,
                    code = data.shipment.code,
                    customerDue = data.shipment.customerDue,
                    createdAt = data.shipment.createdAt,
                    collected = data.shipment.collected,
                    paidToCustomer = data.shipment.paidToCustomer,
                    paidToDeliveryAgent = data.shipment.paidToDeliveryAgent

                };
                return View(shipment);
            }

            return View();

        }


So I need to pass content-language: ‘ar’ where can I put it?

Thanks for help.

1 Answers1

0

Adding your header (just look at last line):

            var options = new GraphQLHttpClientOptions
            {
                EndPoint = new Uri("https:EndPoint/graphql"),
                
                
                // Add any additional configuration options here
            };
            

            var client = new GraphQLHttpClient(options, new NewtonsoftJsonSerializer());
            client.HttpClient.DefaultRequestHeaders
                .TryAddWithoutValidation("content-language", "ar");

Please also look up What are Content-Language and Accept-Language?

enter image description here

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • thanks for response, but i face this errore InvalidOperationException: Misused header name, 'content-language'. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects. – عالم ديم Jul 24 '23 at 00:58
  • Ok, delete `client.HttpClient.DefaultRequestHeaders.Add("content-language", "ar");` and use `client.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("content-language", "ar");` instead. – Bartłomiej Stasiak Jul 24 '23 at 01:11
  • hi and thank you again, your code its working but without any effect, I set the real endpoint if you want try, but the language still not change , thank you – عالم ديم Jul 24 '23 at 13:02
  • If header is present, it looks like new problem, which is related to app, not client. You may create another post about it, where you will describe, how language is managed by your app. – Bartłomiej Stasiak Jul 24 '23 at 15:27