Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERR_INVALID_HTTP_RESPONSE using Angular 7 and ASP.Net Core 2.2 in Google Chrome browser

I have an application which is using Angular 7 for the front end and ASP.Net Core 2.2 for serving APIs. When I am sending POST or PUT requests using Google Chrome browser, I get ERR_INVALID_HTTP_RESPONSE error.

When I downgrade my .Net Core to version 2.1, everything works fine

Also when I am testing my app in Firefox, again everything is working fine!

I do not know how to resolve this issue

I am using Google Chrome version 71

When I check the Network tab in Google Chrome, I found that Chrome sends a preflight request to the server too...

Here is what recorded in Network tab:

First:

Request URL: http://localhost:12346/api/XXXX
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: [::1]:12346
Referrer Policy: no-referrer-when-downgrade

Then:(which cause the error)

Request URL: http://localhost:12346/api/XXXX
Referrer Policy: no-referrer-when-downgrade

As you see chrome does not send any POST request to the server (which should send).

like image 887
Vahid Farahmandian Avatar asked Dec 26 '18 15:12

Vahid Farahmandian


1 Answers

This may have to do with ASP NET Core 2.2 as you figured. There's an issue registered on GitHub https://github.com/aspnet/AspNetCore/issues/4398

And the work-around is as follows - add the following bit of code in your startup.cs class (I've kept this first in Configure method)

app.Use(async (ctx, next) =>
{
  await next();
  if (ctx.Response.StatusCode == 204)
  {
    ctx.Response.ContentLength = 0;
  }
});

Do keep track of the GitHub issue.

like image 54
AD8 Avatar answered Nov 14 '22 02:11

AD8