net::ERR_INVALID_HTTP_RESPONSE error after post request with Angular 7

public public

I am working on small web application and I am using angular 7 and asp.net core web api as a back end. I am trying to make http post request with angular. My service returns string (token) and when I receive it, I want to show it in alert box.

I have tested my service with Postman and everything works as expected.

From the browser my request is successfully mapped to the controller's action method. I have set breakpoint in this method body and it successfully returns token without any problem.

But httpclient returns error:

[object Object]

And in the browser console I see this:

POST https://localhost:44385/api/auth net::ERR_INVALID_HTTP_RESPONSE

I have two methods (for POST and for GET) in a service class injected into component. They look like these:

logIn(username: string, password: string) {

    const encodedCredentials = btoa(username + ':' + password);

    const httpOptions = {
        headers: new HttpHeaders({
            "Authorization": "Basic " + encodedCredentials,
            "Access-Control-Allow-Origin":"*"
        }),
        responseType: 'text' as 'text'
    };

    this.http.post(this.urlBase + 'auth', null , httpOptions)
    .subscribe(result => {
        alert(result);
    }, error => {
        alert(error); // [object Object]
    });
}

get(){
    return this.http.get(this.urlBase + 'auth', {responseType: 'text'});
}

What can be problem?

Update:

HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
error: ProgressEvent
bubbles: false
cancelBubble: false
cancelable: false
composed: false
currentTarget: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
defaultPrevented: false
eventPhase: 0
isTrusted: true
lengthComputable: false
loaded: 0
path: []
returnValue: true
srcElement: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
target: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
timeStamp: 80234.59999999614
total: 0
type: "error"
__proto__: ProgressEvent
headers: HttpHeaders
headers: Map(0) {}
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure response for (unknown url): 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: null
__proto__: HttpResponseBase

Update 2:

Request with Postman:

enter image description here


enter image description here

Update 3: trying to get token several times showed that sometimes post request is successfull and sometimes not...

enter image description here

Justin Kotalik

In ASP.NET Core 2.2, we released a new Server which runs inside IIS for Windows scenarios. The issue you are running into looks like: https://github.com/aspnet/AspNetCore/issues/4398.

When sending the XMLHttpRequest, there is a preflight OPTIONS request which returns a status code of 204. This was incorrectly handled by the IIS server, returning an invalid response to the client.

In your ASP.NET Core application, can you please try the workaround for now:

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

in the beginning of Configure method.

This will also be fixed in the next patch release of ASP.NET Core. I will follow up when the patch is released.

Edit: The latest release (2.2.1) should address this problem: https://dotnet.microsoft.com/download/dotnet-core/2.2. Please try and see if the issue is resolved.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular HTTP POST request

Http response error when sending a post request Angular 4

Getting a full response in Angular http Post request

c# asp .net core HTTP POST Request works with Postman but not with my angular client(404 error)

calling GET request after POST request in Angular

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

Trying to repeat a http request after refresh token with a interceptor in angular 7

Angular 7 post request with body and header

With Angular 7 use HTTP API for POST and command error headers missing

Angular 7 not sending HTTP Post request data

Angular multiple http post requests give me error: "net::ERR_INSUFFICIENT_RESOURCES"

How to fetch data using http post request in Angular 7

Make an http request inside error handling Angular 7 RXJS

Node js POST Request error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Angular 7 Handling large post http request

Angular HTTP POST Request throwing net::ERR_HTTP2_PROTOCOL_ERROR error

POST request success on POSTMAN but not on Angular 7

http-proxy-middleware econnreset error after successful post request

POST request error - net::ERR_CERT_COMMON_NAME_INVALID

System.Net.WebException The remote server returned an error: NotFound with POST JSON request in Windows Phone 7

(CORS error) Can't send POST request (OPTIONS gets denied) with Angular to ASP.net REST services

Angular download file after POST request

Angular: How to check the http code after a post or put request?

Access Modelstate Error - Asp.Net web api identity - Angular 6 - Http Post request

POST request not working in angular CLI version 7

Error Handling in a POST Request in Angular

Send HTTP POST request in .NET

HTTP POST request returns Unauthenticated error in Angular

net::ERR_HTTP2_PROTOCOL_ERROR by http get request angular 15.2