I'm having a blocking issue with HttpClient
in an ASP.NET MVC project where it completely refuses to allow the Content-Type
header. I know that technically it has no meaning, but for the API I'm calling, it's required.
curl -X GET \
https://api.sample-service.com/v1/items \
-H 'content-type: application/json' \
-H 'secret-key: sk_test_12345'
They require these headers and if you leave the Content-type
header out, it will return a BadRequest
. I do not have control over this.
I managed to get this working in .NET Core 2, but it's completely denied in MVC. Here's a sample of the code:
var client = new HttpClient
{
BaseAddress = new Uri("https://api.sample-service.com/v1/")
};
client.DefaultRequestHeaders.Add("secret-key", "my-secret-key");
var content = new StreamContent(Stream.Null);
content.Headers.Add("Content-Type", "application/json");
var request = new HttpRequestMessage(HttpMethod.Get, "items")
{
Content = content
};
var response = await client.SendAsync(request);
The above works in .Net Core but not MVC. It throws a ProtocolViolationException
.
What can I do in MVC to force-include the Content-Type
header in GET requests?
content-type is set as an attribute of HttpWebRequest in MVC
This is how I usually do Web requests in MVC