Skip to content

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

Open

Description

@AmeenKhan12345

Problem

If the URL already has query params and you pass an empty query argument, HTTPClient adds a & with nothing after it:

HTTPClient.new.get("https://example.com/Item?a=1", {})
# requests: GET /Item?a=1&
query argument Request line
nil GET /Item?a=1
{}, [], '' GET /Item?a=1&
{ "b" => "2" } GET /Item?a=1&b=2

An empty query has nothing to add, so I'd expect the URL to stay /Item?a=1, the same as with nil.

Why it matters

Most servers ignore the trailing &, but some don't. We call an external API (MYOB AccountRight) through a wrapper that passes {} as the default query for GET requests. Since October 2026 that API rejects these requests with a 400 ("A value is required but was not present in the request"), because it reads the trailing & as a parameter with no name and no value.

Reproduction

require "httpclient"

uri = URI("https://example.com/Item?a=1")
[nil, {}, [], "", { "b" => "2" }].each do |q|
  puts "#{q.inspect.ljust(14)} #{HTTP::Message.new_request(:get, uri, q).header.create_query_uri}"
end

Output on HTTPClient 2.10.0:

nil            /Item?a=1
{}             /Item?a=1&
[]             /Item?a=1&
""             /Item?a=1&
{"b" => "2"}   /Item?a=1&b=2

Cause

HTTP::Message::Headers#create_query_part (lib/httpclient/http.rb) always appends "&" + query when the URL already has a query, even when the query argument turns into an empty string.

Fix in #492 .

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions