How to use status like is in Postma from code , when this status is not present in response

◇◆丶佛笑我妖孽 提交于 2019-12-11 17:09:06

问题


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

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


回答1:


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;
        }));
}


来源:https://stackoverflow.com/questions/57371989/how-to-use-status-like-is-in-postma-from-code-when-this-status-is-not-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!