Skip to content

Conversation

@ns-fboucault
Copy link

Fixes #1614

Problem

When the HTTP method (POST, GET, etc.) was specified before optional arguments like --auth-type, an AssertionError was raised due to incorrect argument parsing.

Example failing command:

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

Root Cause

Argparse's nargs=OPTIONAL for the METHOD argument doesn't work well with intermixed arguments. When METHOD appears before optional flags, argparse incorrectly parsed:

  • method=None
  • url='POST' (the method string)
  • URL and request items ended up in unparsed arguments

The assertion at line 416 in _guess_method() failed because it expected no request_items when method is None.

Solution

Added a new _fix_argument_order() method that:

  1. Detects when arguments were misparsed (method is None, url looks like HTTP method, unparsed args present)
  2. Correctly reassigns the values: method from url, url from first unparsed arg
  3. Parses remaining unparsed arguments as request items atomically (all or nothing)
  4. Properly initializes request_items if it's None
  5. Runs before _apply_no_options() to prevent errors from unrecognized arguments

Changes Made

  • httpie/cli/argparser.py: Added _fix_argument_order() method (48 lines)
  • tests/test_cli.py: Added 2 unit tests for the fix
  • tests/test_auth.py: Added integration test with bearer auth

Testing

✅ All 71 existing tests pass
✅ New tests verify the fix works correctly
✅ Tested with original failing command from issue #1614
✅ Backward compatibility maintained

Additional Notes

The fix handles two important edge cases:

  1. Atomicity: All request items are validated before any are added to prevent partial parsing
  2. NoneType safety: Initializes request_items to [] if None before extending

Example Commands Now Working

# Original failing command
http POST --auth-type bearer --auth TOKEN https://example.org

# With request items
http POST --auth-type bearer --auth TOKEN https://example.org foo=bar

# Other HTTP methods
http GET --auth-type bearer --auth TOKEN https://example.org
http PUT --verbose https://example.org

Fixes httpie#1614

When the HTTP method (POST, GET, etc.) was specified before optional
arguments like --auth-type, argparse's nargs=OPTIONAL behavior caused
incorrect argument parsing, resulting in an AssertionError.

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

Argparse incorrectly parsed this as:
  - method=None
  - url='POST'
  - URL and request items ended up in unparsed arguments

This commit adds a new _fix_argument_order() method that:
1. Detects when arguments were misparsed
2. Correctly reassigns method, url, and request_items
3. Validates all request items atomically before adding them
4. Properly initializes request_items if None

The fix runs before _apply_no_options() to prevent errors from
unrecognized arguments and maintains backward compatibility with
existing usage patterns.

Tests added:
- test_fix_argument_order_method_before_options
- test_fix_argument_order_method_before_options_with_items
- test_bearer_auth_method_before_options
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