fix: handle options placed between METHOD and URL arguments#1808
Closed
funsaized wants to merge 1 commit intohttpie:masterfrom
Closed
fix: handle options placed between METHOD and URL arguments#1808funsaized wants to merge 1 commit intohttpie:masterfrom
funsaized wants to merge 1 commit intohttpie:masterfrom
Conversation
When options like --auth-type and --auth are placed between the HTTP method and URL (e.g. 'http POST --auth-type bearer --auth TOKEN URL'), argparse fails to associate the URL with the correct positional argument. On Python 3.12 the URL becomes an 'unrecognized argument'; on Python 3.13+ it may be misparsed as a request item. Both paths now detect the misparsing (method=None, url looks like a method name) and rearrange the arguments. Also replaces the bare 'assert not self.args.request_items' in _guess_method with explicit handling that provides a clear error message instead of an AssertionError. Fixes httpie#1614
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1614
When options like
--auth-typeand--authare placed between the HTTP method and URL:argparse fails to associate the URL with the correct positional argument, causing:
unrecognized arguments: https://example.orgerrorAssertionErrorin_guess_method()(bareassertused for input validation)Root cause
argparse.parse_known_argswith intermixed positional and optional arguments misassigns the positionals when named flags appear betweenMETHODandURL. The method string ends up inargs.url, and the actual URL either lands inno_options(Python 3.12) or gets parsed as a request item viaKeyValueArgType(Python 3.13+, sincehttps:matches the:header separator).Changes
httpie/cli/argparser.py:_apply_no_options: Before erroring on unrecognized arguments, detect whenargs.urllooks like an HTTP method name (^[a-zA-Z]+$) andmethodisNone— rearrange the arguments accordingly. Also handles any remaining args as request items._guess_method: Replace the bareassert not self.args.request_itemswith proper handling for the Python 3.13+ path where the URL was parsed intorequest_items. Provides a clear error message for genuinely invalid input instead of crashing.tests/test_cli.py:_guess_methodwithmethod=Noneand URL inrequest_itemsTest results
All existing tests pass (969 passed, 5 skipped, 4 xfailed). The only pre-existing failure is a Big5 charset detection test unrelated to this change.