Skip to content

fix: handle options placed between METHOD and URL arguments#1808

Closed
funsaized wants to merge 1 commit intohttpie:masterfrom
funsaized:fix/options-between-method-and-url-v2
Closed

fix: handle options placed between METHOD and URL arguments#1808
funsaized wants to merge 1 commit intohttpie:masterfrom
funsaized:fix/options-between-method-and-url-v2

Conversation

@funsaized
Copy link
Copy Markdown

Summary

Fixes #1614

When options like --auth-type and --auth are placed between the HTTP method and URL:

http POST --auth-type bearer --auth TOKEN https://example.org

argparse fails to associate the URL with the correct positional argument, causing:

  • Python 3.12: unhelpful unrecognized arguments: https://example.org error
  • Python 3.13: AssertionError in _guess_method() (bare assert used for input validation)

Root cause

argparse.parse_known_args with intermixed positional and optional arguments misassigns the positionals when named flags appear between METHOD and URL. The method string ends up in args.url, and the actual URL either lands in no_options (Python 3.12) or gets parsed as a request item via KeyValueArgType (Python 3.13+, since https: matches the : header separator).

Changes

httpie/cli/argparser.py:

  • _apply_no_options: Before erroring on unrecognized arguments, detect when args.url looks like an HTTP method name (^[a-zA-Z]+$) and method is None — rearrange the arguments accordingly. Also handles any remaining args as request items.
  • _guess_method: Replace the bare assert not self.args.request_items with proper handling for the Python 3.13+ path where the URL was parsed into request_items. Provides a clear error message for genuinely invalid input instead of crashing.

tests/test_cli.py:

  • Unit test for _guess_method with method=None and URL in request_items
  • Integration tests verifying full commands work with options between METHOD and URL

Test 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.

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
@funsaized funsaized closed this Apr 5, 2026
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.

AssertionError when trying to POST with bearer auth

1 participant