使用代理的 Axios 请求无法正确重定向,如下所述:
请求:
const axios = require('axios')
axios.get('http://testapi.testdomain.com/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10', {
proxy: {
host: '127.0.0.1',
port: 1080
}
})
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err)
})
响应:
$ node axios_promise.js
{ Error: connect ECONNREFUSED 127.0.0.1:80
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.17.1' },
method: 'get',
proxy: { host: '127.0.0.1', port: 1080 },
url: 'http://testapi.testdomain.com/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10',
data: undefined },
request:
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
domain: null,
_events:
{ response: [Function: handleResponse],
error: [Function: handleRequestError] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ maxRedirects: 21,
protocol: 'http:',
hostname: '127.0.0.1',
port: null,
path: '/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut',
method: 'GET',
headers: [Object],
agent: undefined,
auth: null,
host: '127.0.0.1',
pathname: '/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc',
search: '?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut',
slashes: true,
hash: null,
query: 'page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut',
href: 'http://127.0.0.1/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut' },
_redirectCount: 1,
_bufferedWrites: [],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Object],
connection: [Object],
_header: 'GET /api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nUser-Agent: axios/0.17.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(outHeadersKey)]: [Object] },
_currentUrl: 'http://127.0.0.1/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut',
_isRedirect: true },
response: undefined }
请注意顶部的响应的错误部分:
{ Error: connect ECONNREFUSED 127.0.0.1:80
at Object.exports._errnoException (util.js:1022:11)
底部的响应部分:
_currentUrl: 'http://127.0.0.1/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut',
_isRedirect: true },
我得到的是http://127.0.0.1/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut
,
我期望的是
http://testapi.testdomain.com/api/articles/personal/01d76a9844ae4a919f02ddedf11a6fcc?page=2&size=10&uc_param_str=dnvebichntwidsudsvpflameefut
- axios版本: v0.17.1
- 环境: node vv8.1.4,windows 10