Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading