-1

I have an DotNet Core API which we are running in Kestrel Server

In the response headers it adds the header "Content-Length" which is causing an issue.

Is there any way to remove the Content-Length Header for all the responses ?

I tried below code But it only removes the "Server: Kestrel" type header.

.ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                               .UseKestrel(options => options.AddServerHeader = false);
                });
user804401
  • 1,990
  • 9
  • 38
  • 71

1 Answers1

0

If I remember correctly, AddServerHeader only corresponds to the type of WebServer, meaning "Kestrel" keyword.

I don't think there is a built-in mechanism to remove "Content-Length", thus you should create a middleware and after the result you should remove this header.

I would also indicate that when removing this header the client will continue reading the response (or wait), so you will need to force close the connection each time, which isn't recommended.

You can further read here: https://stackoverflow.com/a/15995101/303254

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • The main issue I want to remove the Content-Length Header is because when we receive 204 Status code and Content-length as 0 the WSO2 Gateway is failing, I dint understand your point on removing the header causing the client to continue to read the response, will this fail the application ? – user804401 Nov 16 '21 at 10:50