我有一个url,它将自动重定向到另一个URL,我使用以下代码尝试该URL,但似乎不起作用:
var request = require('request');
var url = 'https://racing.hkjc.com/racing/information/Chinese/Trackwork/TrackworkResult.aspx?Horseno=S001';
var r = request.get(url, function (err, res, body) {
console.log(r.uri.href);
console.log(res.request.uri.href);
console.log(this.uri.href);
});
似乎显示旧的URL,而不显示新的URL。
我应该怎么做才能得到正确的结果?
答案 0 :(得分:0)
request
库将自动使用状态30x
进行重定向。如果得到正确的结果,则应该正确设置href
。 (请参见下面的示例。)如果您命中的端点没有以这种方式进行重定向,则可能需要像followAllRedirects: true
一样传递additional options。
const request = require('request');
const options = {
uri: 'https://racing.hkjc.com/racing/information/Chinese/Trackwork/TrackworkResult.aspx?Horseno=S001',
headers: {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Mobile Safari/537.36'
}
};
request(options, (error, response, body) => {
console.log(response.request.uri.href);
});
// Output: https://racing.hkjc.com/racing/information/Chinese/Trackwork/TrackworkOtherResult.aspx?HorseId=HK_2013_S001