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

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

问题描述

我需要使用Node获取 large 文件的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天全站免登陆