Home > Blockchain >  how to get string response as a object from API in angular 9?
how to get string response as a object from API in angular 9?

Time:10-27

When I call API to get particular list of data according to pass id in API that time it properly works and return object as list of data. but if API don't exist provided id in database then it returns string like in spring boot return "Sorry this id doesn't exist in our list";

So that time we can find string response via inspect by the chrome. but how can we get that string in angular so that we can show user response message from API.

CodePudding user response:

You can try the below code snippet. Hope that works for you.

returnObservableAsStringResp(): Observable<any> {
    const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    const requestOptions: Object = {
      headers: headers,
      responseType: 'text'
    }
    return this.http.get<any>(this.apiURL , requestOptions);
 }

CodePudding user response:

public call(id: any) {
    this.Student = [];
    this.httpClient.getfriends(id).subscribe((respo) => {
        for (const d of (respo as any)) {
            this.Student.push({
                id: d.id,
                name: d.name,
                profilepic: d.profilepic,
                address: d.address,
                mobile: d.mobile,
                isfriend: d.isfriend
            });
        }
        console.warn("resp", respo)
    },
        (data: HttpErrorResponse) => {
            alert(data.error.text);
            console.warn(data.error.text);
        }
    )
}
  • Related