Skip to content

HttpRequest.url() logs spaces in path segments as literal plus signs #886

Description

@sylvesterkaczmarek

Description

HttpRequest.url() uses URLEncoder for both query components and path segments:

append(URLEncoder.encode(segment, "UTF-8"))

URLEncoder applies form/query encoding semantics, where a space becomes +. That is valid for the query-string usage in the same method, but a + in a URL path is a literal plus character rather than a space escape.

As a result:

HttpRequest.builder()
    .method(HttpMethod.GET)
    .baseUrl("https://api.example.com")
    .addPathSegment("user name")
    .build()
    .url()

currently returns:

https://api.example.com/user+name

The actual OkHttp transport does not use this string to construct requests. It calls HttpUrl.Builder.addPathSegment("user name"), which sends the path as user%20name. LoggingHttpClient, however, prints request.url(), so the SDK log can show a different request target from the URL that was actually sent.

Expected behavior

HttpRequest.url() should percent-encode spaces in path segments as %20, while preserving the current + encoding for spaces in query parameters.

Impact

This is an observability/debugging correctness issue. Logs produced by LoggingHttpClient can misrepresent path parameters containing spaces, making reproduced requests target a different resource.

Suggested fix

Keep the existing form encoding for query components, but normalize the encoded path-segment result from + to %20. A literal + remains safe because URLEncoder already represents it as %2B.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions