如何在不将其转换为字符串或json的情况下访问Angular 2 http响应主体?

时间:2021-07-13 15:36:13

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.

我想将REST响应复制到blob中,但我无法做到,因为尚未在响应对象中实现blob()和arrayBuffer()。 Response Body是一个私有变量。

...
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?

在实施这些方法之前,我可以使用解决方案吗?

5 个解决方案

#1


47  

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

有一个更简单的解决方案来访问身体作为一个字符串,我没有看到记录在任何地方:

let body = res.text()

#2


9  

Addon to @StudioLE. You may use json() method to return data as json.

加入@StudioLE。您可以使用json()方法将数据作为json返回。

let body = res.json()

#3


6  

Since I found this question while running into the same problem (and Angular's documentation is not updated as of today) you can now use:

因为我在遇到同样的问题时发现了这个问题(并且Angular的文档在今天没有更新),你现在可以使用:

let blob = new Blob([response.arrayBuffer()], { type: contentType });

Another workaround if you for some reason are on an old version of Angular 2 is:

如果由于某种原因你在Angular 2的旧版本上的另一个解决方法是:

let blob = new Blob([(<any> response)._body], { type: contentType });

#4


2  

set the responseType of requestoptions. That will make the response.blob() method work.

设置requestoptions的responseType。这将使response.blob()方法起作用。

        let headers = this.getAuthorizationHeader();
    headers.append("Accept", "application/octet-stream");
    return this.http
        .get(url, new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob }))
        .map((res: Response): Blob => {
            return res.ok ? res.blob() : undefined;
        })

#5


1  

I can't see no other solutions before the following PR is merged:

在合并以下PR之前,我看不到其他任何解决方案:

Whereas you have a compilation error, the field can be used at runtime...

虽然您有编译错误,但该字段可以在运行时使用...

#1


47  

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

有一个更简单的解决方案来访问身体作为一个字符串,我没有看到记录在任何地方:

let body = res.text()

#2


9  

Addon to @StudioLE. You may use json() method to return data as json.

加入@StudioLE。您可以使用json()方法将数据作为json返回。

let body = res.json()

#3


6  

Since I found this question while running into the same problem (and Angular's documentation is not updated as of today) you can now use:

因为我在遇到同样的问题时发现了这个问题(并且Angular的文档在今天没有更新),你现在可以使用:

let blob = new Blob([response.arrayBuffer()], { type: contentType });

Another workaround if you for some reason are on an old version of Angular 2 is:

如果由于某种原因你在Angular 2的旧版本上的另一个解决方法是:

let blob = new Blob([(<any> response)._body], { type: contentType });

#4


2  

set the responseType of requestoptions. That will make the response.blob() method work.

设置requestoptions的responseType。这将使response.blob()方法起作用。

        let headers = this.getAuthorizationHeader();
    headers.append("Accept", "application/octet-stream");
    return this.http
        .get(url, new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob }))
        .map((res: Response): Blob => {
            return res.ok ? res.blob() : undefined;
        })

#5


1  

I can't see no other solutions before the following PR is merged:

在合并以下PR之前,我看不到其他任何解决方案:

Whereas you have a compilation error, the field can be used at runtime...

虽然您有编译错误,但该字段可以在运行时使用...