【问题标题】:Curl HEAD request returns a last-modified header but Node https.request(..., {method: HEAD},...) does notCurl HEAD 请求返回最后修改的标头,但节点 https.request(..., {method: HEAD},...) 不返回
【发布时间】:2023-01-13 04:15:44
【问题描述】:

显然我对 HEAD 请求有些不了解。

这是 URL:“https://theweekinchess.com/assets/files/pgn/eurbli22.pgn”,我将在下面称为 <URL>。

如果我卷曲它,我会在标题中看到最后修改的条目:

curl --head <网址>

HTTP/2 200 
last-modified: Sun, 18 Dec 2022 18:07:16 GMT
accept-ranges: bytes
content-length: 1888745
host-header: c2hhcmVkLmJsdWVob3N0LmNvbQ==
content-type: application/x-chess-pgn
date: Wed, 11 Jan 2023 23:09:14 GMT
server: Apache

但是,如果我使用 https 在 Node 中发出 HEAD 请求,则该信息会丢失:

  https.request(<URL>, { method: 'HEAD' }, res => {
            console.log([<URL>, res.headers])}).end()

这将返回:

 [
  <URL>
  {
    date: 'Wed, 11 Jan 2023 23:16:15 GMT',
    server: 'Apache',
    p3p: 'CP="NOI NID ADMa OUR IND UNI COM NAV"',
    'cache-control': 'private, must-revalidate',
    'set-cookie': [
      'evof3sqa=4b412b5913b38669fc928a0cca9870e4; path=/; secure; HttpOnly'
    ],
    upgrade: 'h2,h2c',
    connection: 'Upgrade, Keep-Alive',
    'host-header': 'c2hhcmVkLmJsdWVob3N0LmNvbQ==',
    'keep-alive': 'timeout=5, max=75',
    'content-type': 'text/html; charset=UTF-8'
  }
]

【问题讨论】:

    标签: node.js curl http-headers http-head


    【解决方案1】:

    我尝试了 axios 而不是 https:

      const response = await axios.head('https://theweekinchess.com/assets/files/pgn/eurbli22.pgn');
      console.log({response: response.headers})
    

    这有效(包括正确的 MIME 类型):

    date: 'Thu, 12 Jan 2023 20:00:48 GMT',
    server: 'Apache',
    upgrade: 'h2,h2c',
    connection: 'Upgrade, Keep-Alive',
    'last-modified': 'Sun, 18 Dec 2022 18:07:16 GMT',
    'accept-ranges': 'bytes',
    'content-length': '1888745',
    'host-header': 'c2hhcmVkLmJsdWVob3N0LmNvbQ==',
    'keep-alive': 'timeout=5, max=75',
    'content-type': 'application/x-chess-pgn'
    

    我也尝试等待 re2.on('end', console.log(res.headers)),但输出与以前相同。

    我将关闭此问题并将其作为“错误”发布到 Node 的站点上。我确定我执行 HEAD 请求的方式需要更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-15
      • 2021-07-14
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 2018-04-16
      相关资源
      最近更新 更多