#Is there a way to cancel an HTTP call (using Tauri http with a JS frontend)?

4 messages · Page 1 of 1 (latest)

frail furnace
#

I see there is a Duration property but no examples of how to use it.
https://tauri.app/v1/api/js/http/#duration

If I make an HTTP call like below, I want to be able to cancel the call if takes too long. The browser "fetch" operation has a builtiin for canceling async calls in progress: https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal

import { getClient } from "@tauri-apps/api/http";  

   const client = await getClient();
   try {
     const response = await  client.post(url, {
      payload: data,
      type: "Json",
      },{
      headers: {
        "Content-Type" : "application/json"
      }
      });
    } catch (err) {

    }
random locust
#

The Duration is just the type for the connectTimeout property which is part of the options object for getClient.

import { getClient } from "@tauri-apps/api/http";
const client = await getClient({
  connectTimeout: 30, // timeout after 30 seconds, a defacto standard for many HTTP clients
});
#

You can also specify a timeout in each query to override the value of the client.

const url = "https://www.example.com";
const options = {
  timeout: 30,
};
try {
  const response = await client.get(url, options);
} catch (e) {
  console.debug("exception during HTTP GET", url, options, e);
}
gloomy compass
#

on top of that, v2 will have a cancelation mechanism similar to the browser one iirc. But until then, icb's answer is the way to go :)