ionic native http post call is not supporting responseType as blob for pdf download from as WebService

1.3k Views Asked by At

I am using ionic5 with Angular. My API returns pdf as Blob object. I am able to get Blob response when using Angular Http(which works on browser)

let headers =
  new HttpHeaders({
    'Accept': 'application/pdf',
    'Content-type': 'application/json',
    'Authorization': 'Bearer token',
    'Access-Control-Allow-Origin': 'localhost'
  });
  return this.httpClient.post(
         apiURL, 
         JSON.stringify(myBodyParamsObj), 
         {headers: headers, responseType: 'blob'}
     );

But I am not able to get response when using ionic native http(which is required to use to run app in device due to CORS issue when using Angular Http). Tried like below-

   this.nativeHttp.setDataSerializer("raw"); // Tried other options like json etc. but none works
   this.nativeHttp.post(apiURL, params, { 'Authorization': myAuthHeaderObj, 'Content-Type': 'application/json',  'Accept': 'application/pdf', responseType: 'blob'});

Also tried this.nativeHttp.sendRequest but didn't get success.

Please help

1

There are 1 best solutions below

1
On

Finally it worked using below piece of code-

if(requestType == 'BLOB'){
      nativeHttpPromise =  this.nativeHttp.sendRequest(
        apiURL,
        {
          method: 'post',
          data: params,
          headers: { Authorization: authHeader },
          responseType: "blob",
          timeout: 5000
        }
      );
     }

So basically , instead of using nativeHttp.post, nativeHttp.sendRequest() willwork as it accepts responseType='blob'