Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Content-Type: application/octet-stream to .Net Core header

I want to accomplish this, is there any trick

HttpClient c = new HttpClient();
c.DefaultRequestHeaders.Add("Content-type", "application/octet-stream"));
like image 679
Nemanja Andric Avatar asked Dec 09 '25 17:12

Nemanja Andric


1 Answers

You should set content headers with HttpContent object, like below.

var client = _clientFactory.CreateClient();

using (var content = new ByteArrayContent(byteData))
{
    content.Headers.ContentType =
        new MediaTypeHeaderValue("application/octet-stream");

    var response = await client.PostAsync(uri, content);

    //code logic here

    //...
}

Besides, for more information about using IHttpClientFactory to create an HttpClient instance in ASP.NET Core, please check: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1

like image 135
Fei Han Avatar answered Dec 12 '25 03:12

Fei Han



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!