简体   繁体   中英

Curl HEAD request returns a last-modified header but Node https.request(..., {method: HEAD},...) does not

Apparently there's something I don't know about HEAD requests.

Here's the URL: 'https://theweekinchess.com/assets/files/pgn/eurbli22.pgn', which I'll refer to as <URL> below.

If I curl this, I see a last-modified entry in the headers:

curl --head <URL>

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

But if I make a HEAD request in Node using https, That information is missing:

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

This returns:

 [
  <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'
  }
]

I tried axios instead of https:

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

And that works (incl. the proper MIME type):

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'

I also tried waiting for re2.on('end', console.log(res.headers)), but same output as before.

I'm going to close this issue and post it instead as a 'bug' on Node's site. I'm sure there's something that needs to be changed in how I'm executing the HEAD request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM