Home > front end >  Angular-Ionic HttpClient Post request to Asp.net Api retruns 400 but swagger returns 200/401
Angular-Ionic HttpClient Post request to Asp.net Api retruns 400 but swagger returns 200/401

Time:12-16

I have a Ionic App and a asp.net Api.

To authorize I send the Login Credentials in a Post request.

Swagger returns this: correct login 200 response

or wrong login 401 response

When sending my Post request like this:

  httpOptions = {
    headers: new HttpHeaders({ 'content-Type': 'application/json', 'accept': '*/*' })
  };

  login(credentials: {username: string; pw: string}): Observable <any>
  {
    return this.http.post('https://localhost:7206/auth/login', credentials, this.httpOptions)
  }

I get a 400 Error

Chrome Network Post

I don't know much about Http requests and all the answers I could find for http 400 errors is change the content type to aplication/json, which I already did.

Any Help/Tipps/Ideas are appreciated.

CodePudding user response:

Your API is, in the request body, expecting properties with the names name and password. But in your angular code it seems like you are sending the request body which only contains properties username and pw.

This is probably the reason why your server is sending a 400 error, which means you were sending a bad request (e.g. when your request body is not matching the expected schema).

  • Related