node.js - 在 Node 中仅获取 GET 请求的 header

标签 node.js http get http-headers

我需要使用 Node 获取大型文件的 Content-LengthContent-Type header 。

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

我正在寻找类似这样的 python 代码:

import requests

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

stream=True 参数表示文件不会完全下载。

最佳答案

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

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.js - 在 Node 中仅获取 GET 请求的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44763985/

相关文章:

node.js - Azure Functions - 如何支持 ES6 导入/导出

http - 如何在不同的方案 (http/https) 下访问 HTML5 session 存储

javascript - 无法在 node.js 中代理 PDF 文件

java - Spring RestTemplate - 在 GET 中传入对象参数

algorithm - Restful : Using POST to execute algorithms

jquery - AJAX .post 异步

node.js - 获取字段不是特定字符串的文档

node.js - 在 Node js 中安装 aws-sdk 返回 npm 错误

javascript - node.js 同步版的http.get

javascript - 从nodejs服务器将数据发送到另一个html中的对象