From e49b5132face4271395230bffab11fd1a9dd5734 Mon Sep 17 00:00:00 2001 From: Tim Vergenz Date: Wed, 27 Apr 2022 13:20:39 -0400 Subject: [PATCH 1/2] wip implementation of --base-url argument Addresses httpie/httpie#838 --- httpie/cli/argparser.py | 2 ++ httpie/cli/definition.py | 12 ++++++++++++ tests/test_cli.py | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index 9bf09b3b..dcc9f4ff 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -203,6 +203,8 @@ class HTTPieArgumentParser(BaseHTTPieArgumentParser): } def _process_url(self): + self.args.url = self.args.base_url + self.args.url + if self.args.url.startswith('://'): # Paste URL & add space shortcut: `http ://pie.dev` → `http://pie.dev` self.args.url = self.args.url[3:] diff --git a/httpie/cli/definition.py b/httpie/cli/definition.py index 0e5f91ed..a56f1d51 100644 --- a/httpie/cli/definition.py +++ b/httpie/cli/definition.py @@ -78,6 +78,8 @@ positional_arguments.add_argument( $ http :3000 # => http://localhost:3000 $ http :/foo # => http://localhost/foo + Prefixed with --base-url before default scheme or shorthand processing take place. + """, ) positional_arguments.add_argument( @@ -703,6 +705,16 @@ network.add_argument( action='store_true', short_help='Build the request and print it but don’t actually send it.' ) +network.add_argument( + '--base-url', + default='', + short_help='String to prepend to the request URL.', + help=""" + String to prepend to the request URL before --default-scheme and/or localhost + shorthand are processed. If specified multiple times, last value is used. + + """ +) network.add_argument( '--proxy', default=[], diff --git a/tests/test_cli.py b/tests/test_cli.py index 6504c8a9..e9626d27 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -375,3 +375,8 @@ class TestSchemes: def test_scheme_when_invoked_as_https(self, httpbin_secure): url = f'{httpbin_secure.host}:{httpbin_secure.port}' assert HTTP_OK in http(url, program_name='https') + + +class TestBaseUrl: + def test_base_url(self): + assert False, 'Needs tests' From 71b1e0a161f76016b0c57134572092727afdd089 Mon Sep 17 00:00:00 2001 From: Tim Vergenz Date: Mon, 9 May 2022 11:06:14 -0400 Subject: [PATCH 2/2] switch METHOD to option-only argument --- httpie/cli/argparser.py | 28 ---------------------------- httpie/cli/definition.py | 35 +++++++++++++++++------------------ 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index dcc9f4ff..4d8986ad 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -414,39 +414,11 @@ class HTTPieArgumentParser(BaseHTTPieArgumentParser): """ if self.args.method is None: - # Invoked as `http URL'. - assert not self.args.request_items if self.has_input_data: self.args.method = HTTP_POST else: self.args.method = HTTP_GET - # FIXME: False positive, e.g., "localhost" matches but is a valid URL. - elif not re.match('^[a-zA-Z]+$', self.args.method): - # Invoked as `http URL item+'. The URL is now in `args.method` - # and the first ITEM is now incorrectly in `args.url`. - try: - # Parse the URL as an ITEM and store it as the first ITEM arg. - self.args.request_items.insert(0, KeyValueArgType( - *SEPARATOR_GROUP_ALL_ITEMS).__call__(self.args.url)) - - except argparse.ArgumentTypeError as e: - if self.args.traceback: - raise - self.error(e.args[0]) - - else: - # Set the URL correctly - self.args.url = self.args.method - # Infer the method - has_data = ( - self.has_input_data - or any( - item.sep in SEPARATOR_GROUP_DATA_ITEMS - for item in self.args.request_items) - ) - self.args.method = HTTP_POST if has_data else HTTP_GET - def _parse_items(self): """ Parse `args.request_items` into `args.headers`, `args.data`, diff --git a/httpie/cli/definition.py b/httpie/cli/definition.py index a56f1d51..97a5b306 100644 --- a/httpie/cli/definition.py +++ b/httpie/cli/definition.py @@ -47,24 +47,6 @@ positional_arguments = options.add_group( Only URL is required. """, ) - -positional_arguments.add_argument( - dest='method', - metavar='METHOD', - nargs=Qualifiers.OPTIONAL, - default=None, - short_help='The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).', - help=""" - The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...). - - This argument can be omitted in which case HTTPie will use POST if there - is some data to be sent, otherwise GET: - - $ http example.org # => GET - $ http example.org hello=world # => POST - - """, -) positional_arguments.add_argument( dest='url', metavar='URL', @@ -699,6 +681,23 @@ authentication.add_argument( network = options.add_group('Network') +network.add_argument( + '--method', + '-X', + metavar='METHOD', + default=None, + short_help='The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).', + help=""" + The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...). + + This argument can be omitted in which case HTTPie will use POST if there + is some data to be sent, otherwise GET: + + $ http example.org # => GET + $ http example.org hello=world # => POST + + """, +) network.add_argument( '--offline', default=False,