5

How do I get the service code? I try the code below, but it does not log anything.

In a service.ts file.

constructor(private http: HttpClient) { }

postForgotPass(email): Observable<Response> {

return this.http.post<Response>(envi.apiUrl +'/user/forgotpassword', {
  "email": email,
  "headers": headers,
  observe: 'response'

})
}

In my component.ts file

sendForgotPass() {
 return this.service.postForgotPass(this.emailFormControl.value)
    .subscribe(
      res => {
        console.log(res.status);
      })
    }
2

4 Answers 4

6

From Angular docs:

getConfigResponse(): Observable<HttpResponse<Config>> {
  return this.http.get<Config>(
    this.configUrl, { observe: 'response' });
}

Usage

this.configService.getConfigResponse()
    // resp is of type `HttpResponse<Config>`
    .subscribe(resp => {
      // display its headers
      const keys = resp.headers.keys();
      this.headers = keys.map(key =>
        `${key}: ${resp.headers.get(key)}`);

      // access the body directly, which is typed as `Config`.
      this.config = { ... resp.body };
    });
Sign up to request clarification or add additional context in comments.

Comments

2

It is because non 2xx status codes are mapped to an error. You need to hook provide a handler for second parameter of subscribe(errors)

sendForgotPass() {
    return this.service.postForgotPass(this.emailFormControl.value)
        .subscribe(res => console.log(res.status), err => console.log('error', err.status))
    }

2 Comments

This work, but even 2xx codes are logging as "error:200". Why is that?
Something else must be wrong. Try logging what is in err variable. Maybe some deserialisation or content type is wrong. Check network tab of browser that you get what you expect.
0
if(response === undefined || response === ''  || response === null)
{  
    this.isStatusCode[i] = false;
}

Comments

0

`import { HttpClient } from "@angular/common/http";

constructor( private httpClient: HttpClient){}

this.httpClient.get<any>(this.API_URL, {observe: 'response'})
        .subscribe({
          next: (res) => {
console.error(res.status)  // api status in header
console.error(res.body) // response from api
}
})`

{observe: 'response'} in get method gives full response object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.