Skip to content

Duplicate query param keys are stripped when using URLSearchParams #1814

@rosswilson

Description

@rosswilson

Describe the bug

  • Node.js version: v16.4.0
  • OS & version: Mac 10.15.7
  • Got version: 12.0.0-beta.1

Actual behavior

When passing a URLSearchParams with duplicate keys, only the last key/value is used in the URL search params.

  const searchParams = new URLSearchParams();

  searchParams.append("a", "100");
  searchParams.append("a", "200");
  searchParams.append("a", "300");

  const response = await got.get(
    'https://example.com',
    {
      searchParams: searchParams,
    }
  );

  console.log(response.request.options.url);

Results in the following URL:

  URL {
      href: 'https://example.com?a=300',
      origin: 'https://example.com',
      protocol: 'https:',
      username: '',
      password: '',
      host: 'example.com',
      hostname: 'example.com',
      port: '',
      pathname: '/',
      search: '?a=300',
      searchParams: URLSearchParams { 'a' => '300' },
      hash: ''
    }

Expected behavior

I expected the duplicate search param keys to have been included. The same happens even if I toString() the URLSearchParams myself and pass that in. Seems like Got is parsing and re-encoding the params.

  URL {
      href: 'https://example.com?a=100&a=200&a=300',
      origin: 'https://example.com',
      protocol: 'https:',
      username: '',
      password: '',
      host: 'example.com',
      hostname: 'example.com',
      port: '',
      pathname: '/',
      search: '?a=100&a=200&a=300',
      searchParams: URLSearchParams { 'a' => '100', 'a' => '200', 'a' => '300' },
      hash: ''
    }

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething does not work as it should

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions