petko_stankoski
petko_stankoski

Reputation: 10713

Http failure for getting base64 string

This is my code:

getPhoto(employeeId: string): Observable<any[]> {
    const headers = new HttpHeaders({
      'responseType': 'json'
    })

    const url = `/Emps/GetPhoto`;
    const params = new HttpParams({
      fromObject: { employeeId },
    });
    return this.http.get<any[]>(url, { params, headers });
  }

And this is what is returned from that method:

data:image/svg;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2aWV3Qm94PSIwIDAgNDY4IDI4MCIgd ...

The method returns Success 200, however in console I get SyntaxError: Unexpected token d in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad, and the getPhoto subscribe debugger is never hit. What am I doing wrong?

Upvotes: 1

Views: 846

Answers (1)

Barremian
Barremian

Reputation: 31125

The default response type for Angular HttpClient's GET method is json. Try to set the response type explicitly

return this.http.get<any[]>(url, { params, headers }, responseType: 'text' });

You could also use blob or arraybuffer if it's more appropriate to your requirement. It's type definition is as follows

responseType?: 'arraybuffer'|'blob'|'json'|'text'

Update 1: pushed responseType outside the options object

Upvotes: 3

Related Questions