Skip to content

Commit 71b1e0a

Browse files
committed
switch METHOD to option-only argument
1 parent e49b513 commit 71b1e0a

File tree

2 files changed

+17
-46
lines changed

2 files changed

+17
-46
lines changed

httpie/cli/argparser.py

-28
Original file line numberDiff line numberDiff line change
@@ -414,39 +414,11 @@ def _guess_method(self):
414414
415415
"""
416416
if self.args.method is None:
417-
# Invoked as `http URL'.
418-
assert not self.args.request_items
419417
if self.has_input_data:
420418
self.args.method = HTTP_POST
421419
else:
422420
self.args.method = HTTP_GET
423421

424-
# FIXME: False positive, e.g., "localhost" matches but is a valid URL.
425-
elif not re.match('^[a-zA-Z]+$', self.args.method):
426-
# Invoked as `http URL item+'. The URL is now in `args.method`
427-
# and the first ITEM is now incorrectly in `args.url`.
428-
try:
429-
# Parse the URL as an ITEM and store it as the first ITEM arg.
430-
self.args.request_items.insert(0, KeyValueArgType(
431-
*SEPARATOR_GROUP_ALL_ITEMS).__call__(self.args.url))
432-
433-
except argparse.ArgumentTypeError as e:
434-
if self.args.traceback:
435-
raise
436-
self.error(e.args[0])
437-
438-
else:
439-
# Set the URL correctly
440-
self.args.url = self.args.method
441-
# Infer the method
442-
has_data = (
443-
self.has_input_data
444-
or any(
445-
item.sep in SEPARATOR_GROUP_DATA_ITEMS
446-
for item in self.args.request_items)
447-
)
448-
self.args.method = HTTP_POST if has_data else HTTP_GET
449-
450422
def _parse_items(self):
451423
"""
452424
Parse `args.request_items` into `args.headers`, `args.data`,

httpie/cli/definition.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,6 @@
4747
Only URL is required.
4848
""",
4949
)
50-
51-
positional_arguments.add_argument(
52-
dest='method',
53-
metavar='METHOD',
54-
nargs=Qualifiers.OPTIONAL,
55-
default=None,
56-
short_help='The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).',
57-
help="""
58-
The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).
59-
60-
This argument can be omitted in which case HTTPie will use POST if there
61-
is some data to be sent, otherwise GET:
62-
63-
$ http example.org # => GET
64-
$ http example.org hello=world # => POST
65-
66-
""",
67-
)
6850
positional_arguments.add_argument(
6951
dest='url',
7052
metavar='URL',
@@ -699,6 +681,23 @@ def format_auth_help(auth_plugins_mapping, *, isolation_mode: bool = False):
699681

700682
network = options.add_group('Network')
701683

684+
network.add_argument(
685+
'--method',
686+
'-X',
687+
metavar='METHOD',
688+
default=None,
689+
short_help='The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).',
690+
help="""
691+
The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).
692+
693+
This argument can be omitted in which case HTTPie will use POST if there
694+
is some data to be sent, otherwise GET:
695+
696+
$ http example.org # => GET
697+
$ http example.org hello=world # => POST
698+
699+
""",
700+
)
702701
network.add_argument(
703702
'--offline',
704703
default=False,

0 commit comments

Comments
 (0)