How I can pass GraphQL header in ASP.NET Core 6?

How I can pass GraphQL header in ASP.NET Core 6?


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.

Share
Improve this question

1 Answer
1

Reset to default


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?

How I can pass GraphQL header in ASP.NET Core 6?

Share
Improve this answer

4

  • 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 at 0: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 at 1: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 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 at 15:27



Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *