Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get access to the Angular 2 http response body without converting it to string or json?

Tags:

I would like to copy a REST response into a blob but I am unable to do some because blob() and arrayBuffer() have not yet been implemented in the Response Object. The Response Body is a private variable.

... return this.http.get(url, {params: params, headers: headers})      .map(res => {            // can't access _body because it is private         // no method appears to exist to get to the _body without modification                      new Blob([res._body], {type: res.headers.get('Content-Type')});      })      .catch(this.log); ... 

Is there a solution I can use until these methods get implemented?

like image 891
sschueller Avatar asked Mar 18 '16 14:03

sschueller


People also ask

How would you write code to modify the response from an http GET?

catch( (error: Response) => { return Observable. throw(error); } );

What is the return type of http get in angular?

Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received.


1 Answers

There's a much simpler solution to accessing the body as a string which I haven't seen documented anywhere:

let body = res.text() 
like image 85
StudioLE Avatar answered Oct 10 '22 09:10

StudioLE