Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axios not respecting the value I set for "timeout" #1503

Closed
arthurmmedeiros opened this issue Apr 25, 2018 · 10 comments
Closed

Axios not respecting the value I set for "timeout" #1503

arthurmmedeiros opened this issue Apr 25, 2018 · 10 comments

Comments

@arthurmmedeiros
Copy link

Summary

Hi there,

I've been using axios a lot in my web application. Everything has been working just fine in most cases.
Today, I've got a new demand in my application using axios, but I can't get it working.

I am creating the possibility to import data to my application. The user uploads a .csv file to my server, and with axios I am calling a method in my controller to process data.

The issue I am facing is related to the timeout parameter set when using axios.

I have a large amount of data to import, so it will take more than 2 minutes to process all the data.
Whatever value bigger than 2 minutes won't be considered by axios. It only waits for 2 minutes, and them returns an error.

Am I missing something here? I checked the documentation and made sure I was using what is recommended in "Config order of precedence", but I still can't make it work.

Would someone help me out?

Context

  • axios version: e.g.: v0.17.1
  • Environment: e.g.: node v6.9.4, chrome 54, windows 7, react JS, C#, .net Core

My implementation

axios({
  method: 'post',
  url: '/user',
  timeout: 180000, // Let's say you want to wait at least 180 seconds
  data: {
    id: '1234',
  }
})
.then(function (response) {
      console.log(response);
})
.catch(function (error) {
    console.log(error);
});
@coolgod
Copy link

coolgod commented Apr 26, 2018

timeout depends on both client side and server side, do you want to check your server side config first? e.g., tomcat timeout configs? or sth like that?

in short, while client is still waiting, the server shouldn't close that connection.

you could verify this by running a dummy nodejs server which wait, the timeout actually works:
server.js

const http = require('http');

const server = http.createServer(function(req, res) {
    console.log('req incoming');
    setTimeout(function() {
        res.write('Hello World!');
        res.end();
    }, 60 * 3 * 1000); // after 3 min
})
.listen(9000);
console.log(server.timeout); // default is 1200000

server.timeout = 60 * 4 * 1000; // 4 min

client.js

const axios = require('axios');

console.log(new Date().toUTCString());
axios({
    method: 'post',
    url: 'http://127.0.0.1:9000',
    timeout: 60 * 4 * 1000, // Let's say you want to wait at least 4 mins
    data: {
      id: '1234',
    }
  })
  .then(function (response) {
        console.log(new Date().toUTCString());
        console.log(response.data);
  })
  .catch(function (error) {
        console.log(error);
  });

@arthurmmedeiros
Copy link
Author

Hi @coolgod ,
First of all, thank you so much for the help.

You were right, the problem was in the server. I didn't consider that possibility before because even when the connection was closed the application kept processing the data.

Here is what I did to solve the issue:
In the webconfig file in my project I simply added a parameter called "requestTimeout" and set it to "00:50:00".
<aspNetCore requestTimeout="00:50:00" ..... />

There is one more thing, I don't know why, but I was only able to test it after publishing the application to IIS. The test does not work when debbuging the application.

Again,
Thank you so much @coolgod !

@philjoseph
Copy link

You should mark this issue as resolved, shouldn't you ?

@ziszo
Copy link

ziszo commented Dec 6, 2018

Adding timeout in the config won't work.
Use the following instead:
axios.defaults.timeout = 180000;

@sagomezza
Copy link

@ziszo this works in general or I have to use it with every single request?

@ziszo
Copy link

ziszo commented Dec 17, 2018

@sagomezza:

You have to set it for every instance of axios.

In the case below, buildAxios() is called for each request.

public yourAPIcall(whateverData: any): AxiosPromise<Token> {
    this.http = this.buildAxios();
    const path = '/apiService';
    const config: AxiosRequestConfig = {
      ...axiosConfig,
      ...{
        url: path,
        method: 'POST',
        data: whateverData,
      },
    };
    return this.http.request<Token>(config);
}
private buildAxios(): AxiosInstance {
    const x = axios.create(this.axiosInfo);
    x.defaults.timeout = 180000;
    return x;
}

P.S. Above code is typescript. Don't confuse with your code.
Btw, you only need the buildAxios() snippet.

@Marcholio
Copy link

So is this resolved now?

@ouzhou
Copy link

ouzhou commented Mar 5, 2019

@Marcholio yes, use axios.defaults.timeout = 3000;
if you dont set this value, chrome will show networkerror after long time

@supriyamalusare
Copy link

I have used axios.defaults.timeout = 5000 in React Native project. And it is worked.

@hnrqsss
Copy link

hnrqsss commented Nov 19, 2019

`
function timeout(promise, milliseconds = null) {

const time = !milliseconds ? 15000 : milliseconds

return new Promise((resolve, reject) => {
    
    const id = setTimeout(() => reject( new Error('timeout')), time)
    
    promise.then(response => {
        clearTimeout(id)
        return resolve(response)
    }, reject)
})

}
`

abuiles pushed a commit to stellar/js-stellar-sdk that referenced this issue Jan 30, 2020
* Revert PR https://github.com/stellar/js-stellar-sdk/pull/465/files

* Use axios CancelToken to ensure timeout

Axios timeout doesn't catch missing urls, e.g. those with no response so we use the axios cancel token to ensure the timeout.

axios/axios#647
axios/axios#1503

* adjustments to get tests to pass
@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants