Skip to content

[fix] request does not support IPv6 addresses #1828

Open
@davidje13

Description

@davidje13

Describe the bug

Node.js version: 23.4.0

OS version: Ubuntu 24.10 / Mac OS 15.3

Description:

This line tries to set the host option in a call to http.request to the hostname from the requested URL, but URL.hostname includes [] wrapping, and http.request's host fails when this is provided:

new URL('http://[::1]:8080').hostname // [::1]

http.request({ port: 8080, host: '[::1]' }) // fails

http.request({ port: 8080, host: '::1' }) // fine

I would list this as a feature request rather than a bug, but it's clear from nearby code that IPv6 is intended to be supported already.

Code to reproduce

This was originally reported to me here davidje13/superwstest#18 and narrowed down to when supertest is invoked with an IPv6 URL. A minimal reproduction using only superagent:

import { createServer } from 'node:http';
import superagent from 'superagent';

const server = createServer((req, res) => { res.statusCode=200; res.end('hi'); });
server.listen(8080, '::1', async () => {
  await superagent.get('http://[::1]:8080');
});

Actual behavior

Error: getaddrinfo ENOTFOUND [::1]
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '[::1]',
  response: undefined
}

Expected behavior

Successful request to server

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions