Skip to content

fix: percent-encode spaces in request path URLs - #887

Open
sylvesterkaczmarek wants to merge 2 commits into
openai:mainfrom
sylvesterkaczmarek:fix/http-request-path-spaces
Open

fix: percent-encode spaces in request path URLs#887
sylvesterkaczmarek wants to merge 2 commits into
openai:mainfrom
sylvesterkaczmarek:fix/http-request-path-spaces

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Make HttpRequest.url() encode spaces in path segments as %20 instead of +, matching URL path semantics and the URL actually constructed by the OkHttp transport.

Fixes #886.

Problem

HttpRequest.url() currently uses URLEncoder for both path segments and query parameters.

For query parameters, form encoding is appropriate and a space becomes +.

For path segments, however, + is a literal plus character. A request containing:

.addPathSegment("user name")

currently renders as:

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

The OkHttp transport itself constructs the request with HttpUrl.Builder.addPathSegment, so the actual wire URL uses:

https://api.example.com/user%20name

LoggingHttpClient prints request.url(), which means SDK logs can show a different request target from the one actually sent.

Fix

Keep the existing URLEncoder behavior for query components, but normalize the encoded path-segment space representation:

URLEncoder.encode(segment, "UTF-8").replace("+", "%20")

A literal plus remains unambiguous because URLEncoder already encodes it as %2B before the replacement is applied.

Regression coverage

Updated HttpRequestTest to verify:

  • a path segment containing a space renders with %20;
  • a literal + path character renders as %2B;
  • the existing query-space case remains hello+world.

Validation

  • branch is based directly on current upstream main at cf942a40074291290634321ad9fe21e514030b4c;
  • branch is 0 commits behind upstream;
  • production diff is exactly 1 addition / 1 deletion in HttpRequest.kt;
  • the actual OkHttp request-construction path is unchanged.

Full repository validation is left to GitHub Actions.

Risk

Low. This changes only the diagnostic/string rendering of path-segment spaces. Query encoding and transport request construction are unchanged.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 18, 2026 15:39
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.

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

1 participant