Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion httpie/cli/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,16 @@ def _guess_method(self):
"""
if self.args.method is None:
# Invoked as `http URL'.
assert not self.args.request_items
if self.args.request_items and self.args.url is None:
# Promote what argparse put in request_items[0] as the URL.
self.args.url = self.args.request_items.pop(0)
elif self.args.request_items:
# url is already set AND request_items is non-empty: genuine error.
self.error(
"Could not determine the request URL. "
"Make sure the URL comes directly after the HTTP method:\n"
f" http {self.args.method} URL [REQUEST_ITEM ...]"
)
if self.has_input_data:
self.args.method = HTTP_POST
else:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,24 @@ def test_guess_when_method_set_but_invalid_and_item_exists(self):
KeyValueArg(
key='old_item', value='b', sep='=', orig='old_item=b'),
]

def test_guess_when_method_set_and_url_in_request_items(self):
"""Regression test for https://github.com/httpie/cli/issues/1614.
When optional flags appear between METHOD and URL, argparse can
land the URL in request_items. _guess_method should recover gracefully.
"""
self.parser.args = argparse.Namespace()
self.parser.args.method = 'POST'
self.parser.args.url = None # url not yet parsed
self.parser.args.request_items = ['https://example.org'] # URL ended up here
self.parser.args.ignore_stdin = False
self.parser.env = MockEnvironment()

self.parser._guess_method()

assert self.parser.args.method == 'POST'
assert self.parser.args.url == 'https://example.org'
assert self.parser.args.request_items == []


class TestNoOptions:
Expand Down
Loading