admin管理员组文章数量:1293455
I facing an error while building a jhipster project that says that I need to change the parameter responseType
in a get
request.
Here's the error:
Argument of type '{ responseType: "arraybuffer"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'responseType' are inpatible. Type '"arraybuffer"' is not assignable to type '"json"'. Version: typescript 2.7.2
As you see typescript
is telling me to change the value from arraybuffer
to json
, but I need to keep it as arraybuffer
since I am dealing with a file and I fully aware that the responseType
can be set to arraybuffer
, blob
, json
or text
.
Here's the code
showfile(file?: any): Observable<any> {
return this.http.get<any>(SERVER_API_URL + `leave/api/downloadFile/${file}`, { responseType: 'arraybuffer' });
}
I facing an error while building a jhipster project that says that I need to change the parameter responseType
in a get
request.
Here's the error:
Argument of type '{ responseType: "arraybuffer"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'responseType' are inpatible. Type '"arraybuffer"' is not assignable to type '"json"'. Version: typescript 2.7.2
As you see typescript
is telling me to change the value from arraybuffer
to json
, but I need to keep it as arraybuffer
since I am dealing with a file and I fully aware that the responseType
can be set to arraybuffer
, blob
, json
or text
.
Here's the code
showfile(file?: any): Observable<any> {
return this.http.get<any>(SERVER_API_URL + `leave/api/downloadFile/${file}`, { responseType: 'arraybuffer' });
}
Share
asked Aug 10, 2018 at 12:27
Neji SoltaniNeji Soltani
1,5114 gold badges24 silver badges43 bronze badges
3 Answers
Reset to default 2this.http.get<any>
Remove the any
When I need to call an endpoint that is returning a file, I usually use the type Observable<HttpResponse<Blob>>
.
Try:
showfile(file?: any): Observable<HttpResponse<Blob>> {
return this.http.get(SERVER_API_URL + `leave/api/downloadFile/${file}`, { responseType: 'blob' });
}
We use an ArrayBuffer for a PDF file; this pattern works for the GET verb:
return this.http.get(url, {responseType: 'arraybuffer'});
Some considerations:
- If the url is an API endpoint that does not return a file stream (e.g. MediaType.APPLICATION_OCTET_STREAM), rather it responds with JSON output, you cannot use a mismatched responseType.
- Typescript does not require an explicit return type: "If no parameter type is defined, TypeScript will default to using any, unless additional type information is available ...." see https://www.w3schools./typescript/typescript_functions.php
- If your editor and/or npm build are throwing errors, you may need to create an object to contain the responseType, like this:
let HTTPOptions:Object = {responseType: 'arraybuffer'};
return this.http.get(url, HTTPOptions );
- You may only require the explicit response type with a POST request, like this:
return this.http.post<ArrayBuffer>(url, requestBody, HTTPOptions );
本文标签: javascriptSet HTTP responseType as arraybufferStack Overflow
版权声明:本文标题:javascript - Set HTTP responseType as arraybuffer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741571907a2386058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论