Skip to content

Don't add a trailing '&' when the query argument is empty - #492

Open
AmeenKhan12345 wants to merge 1 commit into
nahi:masterfrom
AmeenKhan12345:fix-trailing-ampersand-empty-query
Open

AmeenKhan12345 wants to merge 1 commit into
nahi:masterfrom
AmeenKhan12345:fix-trailing-ampersand-empty-query

Conversation

@AmeenKhan12345

Copy link
Copy Markdown

Fixes #491

Problem

When the URL already has a query and the query argument is empty ({}, [] or ''), HTTPClient adds a & with nothing after it:

HTTPClient.new.get("https://example.com/Item?a=1", {})
# before: GET /Item?a=1&
# after:  GET /Item?a=1

Some servers reject the empty parameter this creates. See the issue for a real case and a reproduction.

Change

In create_query_part, the query argument is only appended (with &) when it isn't empty. Nothing else changes:

  • A non-empty query argument is still appended: ?a=1 + { "b" => "2" } → ?a=1&b=2.
  • When the URL has no query, the behaviour is unchanged.

Tests

  • Added test_get_query_with_query_in_uri. It checks the request line for nil, {}, [], '' and { "b" => "2" } on a URL that already has a query. The {}, [] and '' cases fail without the fix.
  • Full suite: 266 tests, 0 failures, 0 errors, also with the CI options --enable-frozen-string-literal --debug-frozen-string-literal.

When the URI already has a query and the query argument is empty
({}, [] or ''), create_query_part still appended "&", so
get("http://example.com/?a=1", {}) requested /?a=1&. Some servers
reject the resulting empty parameter.

Only append the query argument when it is not empty.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty query argument adds a trailing '&' to a URL that already has a query

1 participant