How do I download PDF files with CapacitorHttp


Do to issues with the server setting being controlled by another party who will not add (CORS) I am forced to use CapacitorHttp to access the file Url.

Here is what I have so far


async downloadPDF( FullName: any, URL: any) {
    const options = {
      url: URL,
      headers: {
        'x-authorization-token': 'XXXXXXXXX',
        'access-control-allow-origin': 'https://XXXXXXXXXXXX/',
        'access-control-allow-methods': 'GET,POST,OPTIONS',
        'access-control-allow-headers': 'Content-Type, X-Authorization-Token, Origin',
      params: { }, // can I add a version of responseType: 'blob' here

  const response: HttpResponse = await CapacitorHttp.get(options);
  // Type 'HttpResponse' is missing the following properties from type 'Blob': size, type, arrayBuffer, slice, and 2 more.ts(2740)

    directory: Directory.Documents,
    path: `${FILE_DIR}/${FullName}`,
    blob: response

and the response is

response {"headers":{
  "Etag":"\"632cc89b-1abb7\"","Last-Modified":"Thu,22 Sep 2022 20:42:03 GMT",
  "Content-Disposition":"inline; filename=\"sample upload.pdf\"",
  "Date":"Thu, 24 Nov 2022 16:15:26 GMT",

Is there a way I can add a version responseType: ‘blob’ to CapacitorHttp


Turns out CapacitorHttp causes a lot of issues and for myself it even interfered with @angular/common/http and caused it not to work. I instead used

import { Http, HttpDownloadFileResult } from '@capacitor-community/http';
import { Filesystem, Directory } from '@capacitor/filesystem';

async downloadFile(name: any, URL: any) {
  const options = { url: URL, filePath: `${FILE_DIR}/${name}`, fileDirectory: Directory.Documents, method: 'GET', };
  const response: HttpDownloadFileResult = await Http.downloadFile(options);

This downloaded and saved files on iOS and Android with no CORS issues

Answered By – Thomas Degroot

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published