UnCaught FileTransferError?

I am trying to open a PDF in my ionic application and from my server I am receiving a status code 200 which means it is successfully receiving the data in order to open the PDF.

However, I am receiving this error:

capacitor-runtime.js:81 ERROR Error: Uncaught (in promise): FileTransferError: {“code”:3,“source”:“https://cr-st-web6.almacgroup.com:8725/CSMobileApi/webservices/pdf/169481",“target”:“file:///data/user/0/io.ionic.starter/files/file.pdf”,“http_status”:401,“body”:null,"exception”:null}
at resolvePromise (polyfills.js:3193)
at resolvePromise (polyfills.js:3150)
at polyfills.js:3254
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2785)
at Object.onInvokeTask (vendor.js:53413)
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2784)
at Zone.push…/node_modules/zone.js/dist/zone.js.Zone.runTask (polyfills.js:2557)
at drainMicroTaskQueue (polyfills.js:2963)

I have been assured that this is not a problem with our API and is a runtime error but this has been working in the past and Im unsure what this error is telling me.

Here is my function to get and open the pdf:


     const loading = await this.loadingController.create({
       message: 'Loading ' + msg + ' report...'
     });

     await loading.present();

     // Get Access token from storage

     this.storage.get(ACCESS_TOKEN).then(token => {
       console.log('token from storage: ', token);

       const urlOrderNo = this.apiUrl + pathExtension + ID;
       console.log(urlOrderNo);

       this.nativeHttp.get(urlOrderNo, {}, { Authorization: 'Bearer ' + token})
       .then(data => {
         console.log('status: ', data.status);
         console.log('data: ', data.data); // data from server

         loading.dismiss();

         if (data.status === 200) {

          // Get mobile platform
          if (this.platform.is('android')) {

            const uri = encodeURI(urlOrderNo);
            const path = this.file.dataDirectory;
            const transfer = this.ft.create();

            transfer.download(uri, path + 'file.pdf', false,
            {headers: { Authorization: 'Bearer ' + token}})
          .then ( entry => {
            const url = entry.toURL();
            loading.dismiss();
            console.log('url: ' + url);
            this.fileOpener
            .open(url, 'application/pdf')
            .then(() => console.log('File is opened'))
            .catch(e => console.log('Error opening file', e));
          })
          .then(error => {
            console.log(error);
          });

          } else if (this.platform.is('ios')) {

            const uri = encodeURI(urlOrderNo);
            const path = this.file.documentsDirectory;
            const transfer = this.ft.create();
            const setName = Date.now();

            transfer.download(uri, path + `${setName}.pdf`, false,
            {headers: { Authorization: 'Bearer ' + token}})
          .then ( entry => {
            const url = entry.toURL();
            loading.dismiss();
            console.log('url: ' + url);
            this.fileOpener
            .open(url, 'application/pdf')
            .then(() => console.log('File is opened'))
            .catch(e => console.log('Error opening file', e));
          })
          .then(error => {
            console.log(error);
          });

           }

Based on the status code, it’s telling you there’s a problem with authorization, so I would pay special attention to the Authorization header going over the wire.

@rapropos,

Thanks for the response, from my logs I can see that I am receiving a bearer token from storage which is returning a 200 so im not
sure what is the problem with the Authorization headers? Does something else need to be added?

Thanks,

Aaron

imagee7f144.PNG

What I would do is take the stuff that is going over the wire in Chrome’s developer tools network tab and try passing it via netcat or postman and comparing the result. There may also be further information in the server logs, because I would think that 401 is coming from the server somehow. Whenever I’ve seen purely local network errors with Ionic’s network stack, the HTTP status code is 0.

There is no sign of the request in chromes developer tools network tab? I am doing a native.http get request from my API which is receiving
a 200 with the same token that is being used to download the file.

What would be the difference there? Do you think this is a problem coming from my API or an ionic app problem?

Thanks

imagee7f144.PNG

Are you sure? All network activity generated by the app should be in there. You may need to restart the app once connected to see it, though.

It doesnt seem to be there but it is definitely getting to that download point. We had a look at our server logs too and it doesnt seem to be hitting? any ideas where the 401 would be coming from then?

A proxy somewhere in the middle?

@rapropos thanks for the help! turns out my android libraries updated and file-transfer plugin didnt like this so had to revert back. I’m having another problem with session expiry perhaps you know about? here is the link:

Would appreciate your help again!