How to use status like is in Postma from code , wh

2019-08-19 04:21发布

Service.ts

From this function I get like response a JSON like below:

public signupuser(user: Users): Observable<boolean> {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let body = user;
    return this.http.post(Api.getUrl(Api.URLS.signup), body, {
    })
        .pipe(map((response: Response) => {
            console.log('response', response)
            return true;
        }));
}

JSON:

 response {
  "email": "usertest@gmail.com",
  "nome": "user",
  "cognome": "test",
  "abbonato": 0,
  "cityid": "22",
  "msg": "Registered correctly."
  }

When I use it from Postman I get and status 200 OK or any status error

enter image description here

My question is: How to use this status from code, When this status code is not present in JSON file? Or any idea please?

Any idea please?

Update:

How to read and X-JWT that is in header like in image

1条回答
\"骚年 ilove
2楼-- · 2019-08-19 04:48

Looking at your code I believe you are using Angular, by default HttpClient observes the body not response. You will have to set the observe attribute in the options to response then you should be able to read the status.

public signupuser(user: Users): Observable<boolean> {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let body = user;
    return this.http.post(Api.getUrl(Api.URLS.signup), body, { observe: 'response' })
        .pipe(map((response: HttpResponse) => {
            console.log('response', response);
            return true;
        }));
}
查看更多
登录 后发表回答