Skip to content

Conversation

@rodrigobnogueira
Copy link

@rodrigobnogueira rodrigobnogueira commented Jan 15, 2026

Fix Inconsistent Invalid URL Handling (Closes #1832 )

Summary

Fixed an inconsistency where URLs with a scheme but no host (e.g., https:/[google.com](http://google.com/)) raised UnsupportedProtocol instead of InvalidURL.

Problem

Previously, malformed URLs were handled inconsistently:

  • httpx.get('[https://😇](about:blank)')InvalidURL (Correct)
  • httpx.get('https:/[google.com](http://google.com/)')UnsupportedProtocol (Incorrect)

This occurred because https:/[google.com](http://google.com/) (missing one slash) parses as a URL with a scheme but an empty host. Since it lacks a host, it's treated as a relative URL, which httpcore then rejects as an unsupported protocol.

Solution

Added validation in httpx._client.Client._merge_url to explicitly check for URLs that have a scheme but no host. These are now correctly identified as invalid, raising InvalidURL with a descriptive error message.

Changes

  • Modified httpx/_client.py: Added InvalidURL check in _merge_url
  • Added tests/client/test_invalid_url.py: Regression tests for https:/[google.com](http://google.com/) and [https:///google.com](about:blank)

Verification

  • Added new regression tests passing locally
  • Existing tests pass
  • Linting and type checking pass

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
@rodrigobnogueira rodrigobnogueira force-pushed the fix/invalid-url-handling-1832 branch 3 times, most recently from 34c3a80 to 8e6d712 Compare January 15, 2026 05:18
@rodrigobnogueira rodrigobnogueira force-pushed the fix/invalid-url-handling-1832 branch from 8e6d712 to c02979c Compare January 15, 2026 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant