8

I have the following Angular http.service.ts which I use to call the /login API.

login(user: User) {
    console.log("logging in");
    console.log(JSON.stringify(user));
    this.http
        .post<any>(this.loginURL, user, httpOptions)
        .subscribe(
            res => console.log('response : ' + res),
            err => console.log('error : ' + err))
}

This is a really standard POST call but it always returns null and I can't get why. This is the Network Google Chrome's tab information

enter image description here

The Chrome's Console information

enter image description here

I can't get why the response is null. Even though I have no payload, it should at least make the headers available no?

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Open your network tab in chrome dev tool and check the response of the post url, i guess angular has nothing to do with the response but displaying it in the console. "check your backend". – Nour Jun 09 '18 at 23:58
  • actually i did not see the response in the posted photo. – Nour Jun 10 '18 at 00:43

2 Answers2

19

Try the following:

this.http.post("url", body, {headers: headers, observe: "response"})
   .subscribe(res => console.log(res));

You can take the same http options you have but add the observe: response. the header is HttpHeaders from http client. ex: headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Nour
  • 5,609
  • 3
  • 21
  • 24
  • You can take the same http options you have but add the `observe: "response"`. the header is `HttpHeaders` from http client. ex: `headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),` – Nour Jun 10 '18 at 09:00
  • It Works !!!, Although now response.headers is an empty array even though in the Network Google Chrome's tab information I see a lot of them :-( – Juan Medina Dec 06 '18 at 10:12
0

Try the following,

this.http.post(loginUrl,user,httpOptions).map( res =>res).subscribe(data => {
  console.log(data);
});
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Hi, I used the following but it still does not work. My response is still `null` : `.post(this.loginURL, user, httpOptions) .pipe(map(res => res)) .subscribe( res => console.log('response : ' + res)` – Yassin Hajaj Jun 10 '18 at 08:43
  • i need to show message from using 'data' how i access 'data' in my component ? – kumara Feb 15 '19 at 05:49