silkimen / cordova-plugin-advanced-http

Cordova / Phonegap plugin for communicating with HTTP servers. Allows for SSL pinning!
MIT License
391 stars 313 forks source link

how to use abort method of Advanced Http #484

Closed jamalkarami closed 11 months ago

jamalkarami commented 1 year ago

Hi everyone, I hope you are fine.

I have tried to use abort method for aborting a request as written in Readme file.

// start a request and get its requestId
var requestId = cordova.plugin.http.downloadFile("https://google.com/", {
  id: '12',
  message: 'test'
}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry, response) {
  // prints the filename
  console.log(entry.name);

  // prints the filePath
  console.log(entry.fullPath);

  // prints the status code
  console.log(response.status);
}, function(response) {
  // if request was actually aborted, failure callback with status -8 will be invoked
  if(response.status === -8){
    console.log('download aborted');
  } else {
    console.error(response.error);
  }
});

//...

// abort request
cordova.plugin.http.abort(requestId, function(result) {
  // prints if request was aborted: true | false
  console.log(result.aborted);
}, function(response) {
  console.error(response.error);
});

But when I try the same code, It doesn't work, the requestId is underlined in red with this error : L'argument de type 'Promise' n'est pas attribuable au paramètre de type 'string'.

The request methods post|get|put|delete|patch|downloadFile|uploadFile| return a Promise not string id

image

Thanks for any help in how to get requestId ?

chrisjdev commented 1 year ago

Assuming you're also using the @awesome-cordova-plugins wrapper, I found there's methods that end with *Sync that return a string requestId to use with abort, like

let requestId = this.http.sendRequestSync(url, options, (result: HTTPResponse) => console.log('Success!'), (error: any) => console.log('Failure!') );

this.http.abort(requestId);
KaramiJamal commented 1 year ago

Thanks @chrisjdev for your answer, it will helpfull for other developpers.

silkimen commented 11 months ago

Thank you for feedback guys! Closing this to keep track of open issues, but it will be still discoverable for other developers.