Set `args.method` if not specified to either POST or GET based on whether the request has data or not.
(self)
| 407 | 'See https://httpie.io/docs#scripting for details.') |
| 408 | |
| 409 | def _guess_method(self): |
| 410 | """Set `args.method` if not specified to either POST or GET |
| 411 | based on whether the request has data or not. |
| 412 | |
| 413 | """ |
| 414 | if self.args.method is None: |
| 415 | # Invoked as `http URL'. |
| 416 | assert not self.args.request_items |
| 417 | if self.has_input_data: |
| 418 | self.args.method = HTTP_POST |
| 419 | else: |
| 420 | self.args.method = HTTP_GET |
| 421 | |
| 422 | # FIXME: False positive, e.g., "localhost" matches but is a valid URL. |
| 423 | elif not re.match('^[a-zA-Z]+$', self.args.method): |
| 424 | # Invoked as `http URL item+'. The URL is now in `args.method` |
| 425 | # and the first ITEM is now incorrectly in `args.url`. |
| 426 | try: |
| 427 | # Parse the URL as an ITEM and store it as the first ITEM arg. |
| 428 | self.args.request_items.insert(0, KeyValueArgType( |
| 429 | *SEPARATOR_GROUP_ALL_ITEMS).__call__(self.args.url)) |
| 430 | |
| 431 | except argparse.ArgumentTypeError as e: |
| 432 | if self.args.traceback: |
| 433 | raise |
| 434 | self.error(e.args[0]) |
| 435 | |
| 436 | else: |
| 437 | # Set the URL correctly |
| 438 | self.args.url = self.args.method |
| 439 | # Infer the method |
| 440 | has_data = ( |
| 441 | self.has_input_data |
| 442 | or any( |
| 443 | item.sep in SEPARATOR_GROUP_DATA_ITEMS |
| 444 | for item in self.args.request_items) |
| 445 | ) |
| 446 | self.args.method = HTTP_POST if has_data else HTTP_GET |
| 447 | |
| 448 | def _parse_items(self): |
| 449 | """ |