silkimen / cordova-plugin-advanced-http

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

[Bug] [Android] Timeout argument ignored, global argument taken #471

Open ZweiEuro opened 2 years ago

ZweiEuro commented 2 years ago

Describe the bug My app makes 2 kinds of http calls: To a local device which needs 1s timeout in order to quickly detect if it is down. To a server which needs 10s timeout to work reliably since that can take longer than 1s.

I wanted to avoid setting the timeout globally with
this.http.setRequestTimeout(...); But when I set it only via the argumetns of: .sendRequest(...,{... timeout:1}); Its ignored. If a timeout occurs it's always exactly after 60 seconds which is the default.

Setting the global default will overwrite it for all requests.

This makes me think that the argument of the sendrequest timeout is ignored in general and only the global argument is taken.

System info

Are you using ionic-native-wrapper? No, at least i can't find it anywhere in package.json or in config.xml of the app.

Minimum viable code to reproduce


  this.platform.ready().then(async () => {
      // Test timeout bug

      let now = new Date().getTime();

      // will timeout in 60s since that is globally default
      await this.http
        .sendRequest("http://192.168.123.32", {
          method: "get",
          responseType: "blob",
          timeout: 1,
        })
        .then((data) => {
          console.error("Success", data);
        })
        .catch((error) => {
          console.error("timeout after: ", new Date().getTime() - now, "ms", error);
        });

      this.http.setRequestTimeout(2);

      now = new Date().getTime();

      // will timeout in 2s since that is globally default now, both cases ignore the timeout
      await this.http
        .sendRequest("http://192.168.123.32", {
          method: "get",
          responseType: "blob",
          timeout: 1,
        })
        .then((data) => {
          console.error("Success", data);
        })
        .catch((error) => {
          console.error("timeout after: ", new Date().getTime() - now, "ms", error);
        });

      console.warn("Default timout is ", this.http.getRequestTimeout());
    });
ZweiEuro commented 2 years ago

For me this is what is printed afterwards. 2022-06-19-26

silkimen commented 2 years ago

Hi @ZweiEuro, I think we forgot to update the documentation when #404 was implemented. Would you please try to set the values for connectTimeout and readTimeout in your options object instead of timeout? I guess this should fix your problem. I'd appreciate your feedback, so we can fix the documentation.

ZweiEuro commented 2 years ago

Hi @silkimen, Currently i am doing this:

this.http.setRequestTimeout(custom_timeout ? custom_timeout : this.timeout); // This has an effect
this.http
  .sendRequest("http://" + (ip ? ip : this.device.getIP()) + ":" + this.hostPort + "/api/v1/command", {
    method: "post",
    data: body,
    timeout: this.timeout, // this has no effect
    headers: { "Content-Type": "application/json" },
    serializer: "json",
    responseType: "json",
  })

You mean i should be doing this ?

//this.http.setRequestTimeout(custom_timeout ? custom_timeout : this.timeout); // This has an effect
this.http
  .sendRequest("http://" + (ip ? ip : this.device.getIP()) + ":" + this.hostPort + "/api/v1/command", {
    method: "post",
    data: body,
    connectTimeout: this.timeout, // <------
    headers: { "Content-Type": "application/json" },
    serializer: "json",
    responseType: "json",
  })
bluwduch commented 1 year ago

I had the same issue and came here to find this already reported... I tried adding additional properties to the options object connectTimeout and readTimeout and can confirm that fixed the problem.

ZweiEuro commented 1 year ago

I think it fixed my issue as well @bluwduch I am just asking for docs and confirmation

up2-date commented 10 months ago

I have had the same problem, it would be nice if the documentation could be updated.