我有两个测试网址:
我正在尝试执行一个简单的请求承诺请求,以在下载之前检查该图像是否存在。
我的测试脚本:
const rp = require('request-promise');
// const URL = 'https://i.redd.it/izxp34ew4nw11.jpg';
// const URL = 'https://i.imgur.com/JedrSP3.png'; // 404 imgur
const URL = 'https://i.stack.imgur.com/luggv.png' // working imgur
async function main() {
try {
const payload = await testRp();
debugger;
} catch (e) {
debugger;
}
}
async function testRp({ increaseBy } = {}) {
const uri = URL;
const options = {
method: 'HEAD',
uri,
json: true,
};
return rp(options);
}
main();
如果您点击已删除的图像链接,它会将您重定向到他们的
image has been deleted or missing
页面。这里的问题是,如果您以编程方式获取它,则不会获得状态代码。你得到这个:
< { 'last-modified': 'Wed, 14 May 2014 05:44:36 GMT',
< etag: '"d835884373f4d6c8f24742ceabe74946"',
< 'content-type': 'image/png',
< 'cache-control': 'public, max-age=31536000',
< 'content-length': '503',
< 'accept-ranges': 'bytes',
< date: 'Sat, 10 Nov 2018 22:52:05 GMT',
< age: '4336782',
< connection: 'close',
< 'x-served-by': 'cache-iad2145-IAD, cache-yyz8322-YYZ',
< 'x-cache': 'HIT, HIT',
< 'x-cache-hits': '63963, 8479',
< 'x-timer': 'S1541890325.130391,VS0,VE0',
< 'access-control-allow-methods': 'GET, OPTIONS',
< 'access-control-allow-origin': '*',
< server: 'cat factory 1.0' }
您似乎可以继续提出
GET
请求。
有没有办法在不使用imgur API的情况下检查图像是否存在并保证返回的状态正确?
content-length
将始终是
'503'
并且如果您计算返回的
headers
字段
date
和之间的差异
age
,结果总会是
1537553543000
。这显然不是万无一失的,因为它可以随时更改,因此如果您不想使用 Imgur API(有速率限制),您可能需要构建一个 cron 作业来检查这两个数字。