1

I am facing issue in getting server response in error block (callback) of plugin NativeScript/nativescript-background-http in mobile app development for uploading file. On basis on error server response will perform other operation

Error Call back of plugin nativescript-background-http
Uploading the file to server , I have been able to read the response in responded callback but that is a case of success.

task.on("responded", (e:any)=>{
               //response 
                let response= JSON.parse(e.data)
          });

in case of error it is not working

 let task = session.multipartUpload(params, request); 
task.on("error", event => {
                console.dir(event);
                console.log(JSON.stringify(event))
                console.log(event.responseCode);
                console.log(event.response)
               // console.log((JSON.parse(event.response))
            });

result-

eventName: "error" JS: object: { JS: "_observers": { JS:
"complete": [ JS: {} JS: ], JS: "error": [ JS: {} JS: ], JS: "responded": [ JS: {} JS: ] JS: }, JS: "_session": { JS: "_id": "file-upload" JS: }, JS: "_id": "file-upload{1}", JS: "_description": "Log File", JS: "_upload": 4309, JS: "_totalUpload": 4309, JS: "_status": "error" JS: } JS: error: "null" JS: responseCode: "400" JS: response: net.gotev.uploadservice.ServerResponse@73e7f34

Expected : - response : { isSuccess:false, msg:'Token-Invalid' }

1 Answers1

4

The plugin do not parse the response on error event, but you could get the JSON response with following code on Android.

const responseObj = event.response && JSON.parse(event.response.getBodyAsString());
Manoj
  • 21,753
  • 3
  • 20
  • 41