GET请求适用于cUrl,但不适用于axios



我正试图在Axios中设置一个请求。该请求与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

然而,我无法使它与Axios(在同一台服务器上(一起工作。这是我在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)

它等待超时发送以下异常

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.1rnAccept: application/json, text/plain, */*rnUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36rnCookie: anyrnHost: www.nseindia.comrnConnection: closernrn',
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/api/quote-equity?symbol=COALINDIA',
_ended: false,
res: null,
...

我在Axios中做错了什么?

问题可能不在上面给定的代码中,根本原因可能来自您试图连接的外部API。(所花费的响应时间(

API未能在30000ms(30秒(内做出响应

请记录执行API请求前后的时间,以检查从API请求返回错误所需的实际时间。

FYI:错误本身提到了这个问题,Error: timeout of 30000ms exceeded

最新更新