I have the same problem, the api that i consumed only works with "AUTHORIZATION" header and i have no control over it. So the only available solution must be on client side. To allow send case sensitive header i use the internal method "TryAddWithoutValidation" of "HttpRequestHeaders" class and "force" the invocation using reflection.
public static class HttpClientExtensions {
public static void AddCaseSensitive(this HttpRequestHeaders headers, string key, string value)
{
var reqHeaderType = headers.GetType();
var tryAddWithoutValidation=reqHeaderType.GetRuntimeMethods()
.Where(m => m.Name == "TryAddWithoutValidation")
.FirstOrDefault(m=>{
var funcParamenters = m.GetParameters().ToList();
var firstParam = funcParamenters.First();
var secondParam = funcParamenters.Last();
return firstParam.ParameterType.FullName!= typeof(string).FullName && secondParam.ParameterType.Namespace!="System.Collections.Generic";
});
var internalHeaderDescriptorType = tryAddWithoutValidation.GetParameters().First().ParameterType;
var HeaderDescriptor = internalHeaderDescriptorType.GetConstructor(BindingFlags.NonPublic|BindingFlags.Instance,new Type[]{typeof(string)});
object headerDescriptorInstance = HeaderDescriptor.Invoke(new object[] { key });
tryAddWithoutValidation.Invoke(headers, new object?[] { headerDescriptorInstance,value});
} }
To use it
var client = new HttpClient();
client.BaseAddress = new Uri(_config.ApiUrl);
client.DefaultRequestHeaders.AddCaseSensitive("AUTHORIZATION",$"Token {_config.ApiToken}");
this only was tested with System.Net.Http@4.3.0