下面我有两个http请求,一个是request
,另一个是isomorphic-fetch
(node-fetch
)。出于某种原因,request
的请求有效,但node-fetch
正在回复错误代码503
。我在fetch版本中缺少什么?
const URL = require('url')
const fetch = require('isomorphic-fetch')
const HttpsProxyAgent = require('https-proxy-agent')
const request = require('request');
const url = process.env.URL
const proxy = process.env.PROXY
const requestPromise = function (url, options) {
if (/^\/\//.test(url)) {
url = 'https:' + url;
}
return new Promise(function(resolve, reject) {
return request.call(this, url, options, function (err, res, body) {
if (err) {
throw new Error(err);
}
res.ok = true;
res.json = function () {
return JSON.parse(res.body);
}
return resolve(res);
});
});
};
function getProxy (url) {
const parsedProxyURL = URL.parse(url);
parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:';
return parsedProxyURL
}
requestPromise(url, {
agent:new HttpsProxyAgent(getProxy(proxy))
})
.then(console.log)
.catch(console.log)
fetch(url, {
agent:new HttpsProxyAgent(getProxy(proxy))
})
.then(console.log)
.catch(console.log)
答案 0 :(得分:2)
request
模块似乎是自动设置一个标头,host
模块不是fetch
。通过进行此更改,它可以正常工作。
const URL = require('url')
const fetch = require('isomorphic-fetch')
const HttpsProxyAgent = require('https-proxy-agent')
const request = require('request')
function getProxy (url) {
const parsedProxyURL = URL.parse(url)
parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:'
return parsedProxyURL
}
const url = process.env.URL
const proxy = process.env.PROXY
fetch(url, {
headers: {host: URL.parse(url).host},
agent: new HttpsProxyAgent(getProxy(proxy))
})
.then(res => res.json()).then(res => {
console.log(res)
})
.catch(console.log)