在 Node 中仅获取 GET 请求的标头 [英] Fetch only headers of a GET request in Node

查看:39
本文介绍了在 Node 中仅获取 GET 请求的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Node 获取 文件的 Content-LengthContent-Type 标头.

I need to get the Content-Length and Content-Type headers of large files with Node.

不幸的是,我正在处理的服务器不允许 HEAD 请求,而且文件太大而无法发出整个 GET 请求.

Unfortunately, the server I'm dealing with doesn't allow HEAD requests, and the files are too large to make an entire GET request.

我正在寻找类似这个 python 代码的东西:

I'm looking for something like this python code:

import requests

r = requests.get(url, stream=True)
content_length = r.headers['Content-Length']
content_type = r.headers['Content-Type']

stream=True 参数意味着文件不会被完全下载.

The stream=True parameter means that the file won't be fully downloaded.

推荐答案

如果您使用的是 request 包,您可以执行以下操作:

If you're using the request package, you could do something like this:

const request = require('request');
const r = request(url);
r.on('response', response => {
    const contentLength = response.headers['content-length'];
    const contentType = response.headers['content-type'];
    // ...
    r.abort();
});

这篇关于在 Node 中仅获取 GET 请求的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆