-1

I am trying to get logo image(any type image, example: jpeg, png, gif) from API I can see image in Network tab but console throws an error as shown in attached image. Any suggestions? I am using htppclient.get and .toPromise() to and Angular latest version.

edit: I also have this header set

    const headers = new HttpHeaders().set('Content-Type', 'application/json');
    return this.getHeaders().append('responseType', 'text');

enter image description here

3 Answers 3

4

If you are receiving 'blob' type from your request you have to change your headers accordingly. Try changing the accept-type from json to image format. Check below code and see if that helps

getImage() {
    let httpHeaders = new HttpHeaders()
         .set('Accept', "image/webp,*/*");

    return this.http.get<Blob>(url, { headers: httpHeaders, responseType: 'blob' as 'json' });
}

After this you can create the image from this data.

Sign up to request clarification or add additional context in comments.

1 Comment

My header was good but I was missing to create image from that. My issue is resolved by creating image. Thank you for your comment that helped me to find out what I was missing.
3

You should return you http code like this. Blob is the type you want to deal with when working with image file

getImage(imageUrl: string): Observable<Blob> {
  return this.httpClient.get(imageUrl, { responseType: 'blob' });
}

You can view this blog post for more detail

1 Comment

It shows this error now inside of code. Type '"blob"' is not assignable to type '"json"'.ts(2322) client.d.ts(1075, 9): The expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
2

Both answer given below helped me but in my case I was not crating image so once I do GET I than created image and it helped me to solve this issue.

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.