fix(buildURL): preserve encoded colon (%3A) for JSON-stringified params#7290
fix(buildURL): preserve encoded colon (%3A) for JSON-stringified params#7290Amaan-pathan wants to merge 1 commit intoaxios:v1.xfrom
Conversation
This comment was marked as spam.
This comment was marked as spam.
1 similar comment
This comment was marked as spam.
This comment was marked as spam.
|
Thanks for the writeup, and apologies for the slow turnaround. I want to push back on the framing first: That distinction matters because the proposed fix would change behaviour silently for everyone who currently relies on the readable form. Anyone snapshotting URLs, matching them server-side, or just logging them is going to see a sudden shift in JSON-shaped params with no opt-out. A few other concerns with the heuristic itself:
The right answer for strict encoding is already in the public API: axios.get('/foo', {
params: { filter: JSON.stringify({ startedAt: '2025-01-23' }) },
paramsSerializer: { encode: encodeURIComponent }
});That's explicit, scoped, and doesn't change behaviour for anyone else. The reason this isn't more obvious is that it's underdocumented, which is a fair complaint and on us. I've opened #10809 to add a strict-encoding example to the Closing this in favour of the docs fix. Genuinely appreciate the issue being raised — getting the escape hatch documented is the right outcome. |
Description
This PR fixes an issue where colons inside JSON-stringified params were not being consistently encoded. In some environments Axios was converting %3A back to :, while in others it preserved the encoding. This led to unpredictable behavior when APIs expected strictly encoded JSON in query parameters.
To address this, I added a small helper (isJSONLike) that detects when a param value looks like JSON (e.g., "{...}" or "[...]"). If the value is JSON-formatted, the colon remains encoded as %3A. For all non-JSON values, Axios continues to follow its existing encoding rules, so backward compatibility is preserved.
What’s Changed
Added isJSONLike check inside encode()
Ensured JSON-stringified params keep %3A instead of decoding to :
Updated buildURL tests to cover:
JSON-like params
nested objects
arrays, dates, and special characters
custom serializer functions
URLs that already contain a query string