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
Add a ment  | 

3 Answers 3

Reset to default 2
this.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:

  1. 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.
  2. 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
  3. 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 );
  1. 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