'GET Request works with cUrl but not in axios
I am trying to set up a request in Axios. The request works perfectly with cUrl:
curl 'https://www.nseindia.com/api/quote-equity?symbol=COALINDIA' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36' -H 'Cookie: any' --compressed
however I can't make it work with Axios (on the same server). Here is my request in Node:
var axiosOptions = {
method: 'GET',
url: 'https://www.nseindia.com/api/quote-equity?symbol=COALINDIA',
responseType: 'arraybuffer',
timeout: 30000,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
Cookie: 'any'
}
}
await axios(axiosOptions)
It waits for the timeout to send the following exception
Error: timeout of 30000ms exceeded
at createError (/var/www/api/node_modules/axios/lib/core/createError.js:16:15)
at Timeout.handleRequestTimeout [as _onTimeout] (/var/www/api/node_modules/axios/lib/adapters/http.js:217:16)
at listOnTimeout (timers.js:327:15)
at processTimers (timers.js:271:5)
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 30000,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36',
Cookie: 'any' },
method: 'get',
url: 'https://www.nseindia.com/api/quote-equity?symbol=COALINDIA',
responseType: 'arraybuffer',
data: undefined },
code: 'ECONNABORTED',
request:
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
...
_header:
'GET /api/quote-equity?symbol=COALINDIA HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36\r\nCookie: any\r\nHost: www.nseindia.com\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/api/quote-equity?symbol=COALINDIA',
_ended: false,
res: null,
...
What am I doing wrong in Axios?
Solution 1:[1]
The issue might be not in the above given code, the root cause might be from the external API to which you are trying to connect. (the response time it took)
Which the API has failed to respond back within 30,000ms (30 seconds)
Please check for the actual time taken to get the error back from the API request by logging the time before and after performing the API request.
FYI:
Error itself mentions about the issue, Error: timeout of 30000ms exceeded
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jayanga Jayathilake |
