fix(http): keep the existing query string when adding params on web

BR
2mo ago
+44-1

Description

The web implementation of CapacitorHttp.request() always joins params with ?:

const url = urlParams ? `${options.url}?${urlParams}` : options.url;

When url already has a query string, the request goes to a URL with two ?:

CapacitorHttp.get({ url: 'https://example.com/api?a=1', params: { b: '2' } });
// fetches https://example.com/api?a=1?b=2
// the server sees a = "1?b=2" and no b

The Android (HttpRequestHandler.java) and iOS (HttpRequestHandler.swift) implementations keep the existing query and add the params to it, so the same call behaves differently on web. This PR uses & when the URL already contains ?.

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

Any web call that combines a URL with a query string (for example a signed URL or a paging link returned by an API) and params sends a wrong request: the first existing parameter gets the rest of the query appended to its value, and the added params are lost.

Tests or Reproductions

Added core/src/tests/http.spec.ts, which mocks fetch and checks the URL passed to it:

  • appends params to a url without a query string: passes before and after.
  • keeps the existing query string when adding params: fails before the fix (https://example.com/api?a=1?b=2), passes after (https://example.com/api?a=1&b=2).

npm test in core: all suites pass. eslint and prettier --check are clean on the changed files.

Screenshots / Media

N/A

Platforms Affected

  • Android
  • iOS
  • Web

Notes / Comments

This does not touch the array && issue in buildUrlParams (#8270 / #8272); the two changes are independent.

I found this while reading the code with an AI assistant (Claude); I checked the failing test and the fix myself.

🤖 Generated with Claude Code