0

I want to receive response from server. With Postman works great: Request:

{
    "name": "antonio",
    "username": "antonio",
    "email": "antonio@antonio.pl",
    "password": "antonio"
}

Response:

{
    "success": true,
    "message": "User was successfully registered"
}

Im trying to implement same thing in Angular, so I got:

  this.http.post('http://localhost:8090/api/auth/signup',
  JSON.stringify({name: name, username: username, password: password, email: email}),
  {headers: new HttpHeaders({'Content-Type': 'application/json'})})
  .subscribe(res => alert(res[0]));

Checked backend API, works great but alert still is not appearing. Any ideas?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
asddsa
  • 1
  • 1

1 Answers1

0

You shouldn't need to JSON.stringify() the request if you are sending it as 'application/json'. I don't know how your backend interprets it.

this.http.post('http://localhost:8090/api/auth/signup',
  {name: name, username: username, password: password, email: email},
  {headers: new HttpHeaders({'Content-Type': 'application/json'})})
  .subscribe(res => alert(res[0]));

https://angular.io/api/common/http/HttpClient#post

Nicholas Foden
  • 156
  • 3
  • 12
  • That's true, but also got something like: need to add alert('smth) after http.post, because It wont send request – asddsa Aug 16 '18 at 16:56
  • it should sent the post when you subscribe to it maybe try `let signup = this.http.post(...); ` then `signup.subscribe(...` – Nicholas Foden Aug 16 '18 at 21:16
  • also, are you using HttpClient: angular.io/api/common/http/HttpClient or just Http: angular.io/api/http/Http ) – Nicholas Foden Aug 16 '18 at 21:20
  • you should really be using a service containing the observable, Inject it into your Component and subscribe to it there – Nicholas Foden Aug 16 '18 at 21:21