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 Answer
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?
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 useclient.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("content-language", "ar");
instead.– Bartłomiej StasiakJul 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 StasiakJul 24 at 15:27
Not the answer you're looking for? Browse other questions tagged
or ask your own question.
or ask your own question.
|