Node.js HTTP 请求返回没有正文的 header ?

标签 node.js http request axios media

是否有办法让 Node.js 中的 HTTP 请求仅返回响应 header 而不返回正文?特别是 Axios 库?

我想要这样做的原因是因为我想检查用户是否提供了托管媒体文件的 URL,而无需下载整个(可能很大)文件本身。

例如,如果我访问 https://media3.giphy.com/media/3oEduIOpXCpGxagbQY/200w_s.gif ,然后我可以看到 content-type: image/gif 包含在响应 header 中。对于我的应用程序,我只需要检查 content-type header 是否指示媒体文件。下载整个文件就太过分了。

那么我该如何配置 Axios 来做到这一点呢?或者,如果不使用 Axios,一般如何在 Node.js 应用程序中执行此操作?

感谢任何帮助。谢谢

最佳答案

const axios = require('axios');

axios
  .head('https://media3.giphy.com/media/3oEduIOpXCpGxagbQY/200w_s.gif')
  .then(res => console.log(res.headers['content-type']));

使用 head 方法并获取所需的 header 。

关于Node.js HTTP 请求返回没有正文的 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944182/

相关文章:

node.js - 使用 Node 集群与 Redis Bull 后台进程来提高慢速端点性能

node.js - 如何在 react 中显示填充的文档

node.js - 防止 Mongoose findOneAndUpdate 中的 updatedAt 变化

php - 这是使用 PHP CURL 发出 API 请求的最佳方式吗?

node.js - Node 进程在 writeStream 结束前退出

node.js - 在指定文件夹中以编程方式运行 npm install

http - 返回 http 200 OK,响应正文中有错误

json - Python 3.6 : get JSON from aiohttp request

http - 保护 HTTP 请求不被篡改

asp.net - 失败的请求跟踪 : "a failed request trace for this content already exists" 的 IIS/ASP.net 错误