1
0
mirror of https://github.com/httpie/cli.git synced 2025-08-10 22:42:05 +02:00

Request content type

This commit is contained in:
Jakub Roztocil
2020-09-25 14:44:22 +02:00
parent c431ed7728
commit e4e40e5b06
9 changed files with 85 additions and 32 deletions

View File

@@ -18,7 +18,8 @@ from httpie.cli.constants import (
DEFAULT_FORMAT_OPTIONS, HTTP_GET, HTTP_POST, OUTPUT_OPTIONS,
OUTPUT_OPTIONS_DEFAULT,
OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED, OUT_RESP_BODY, PRETTY_MAP,
PRETTY_STDOUT_TTY_ONLY, SEPARATOR_CREDENTIALS, SEPARATOR_GROUP_ALL_ITEMS,
PRETTY_STDOUT_TTY_ONLY, RequestContentType,
SEPARATOR_CREDENTIALS, SEPARATOR_GROUP_ALL_ITEMS,
SEPARATOR_GROUP_DATA_ITEMS, URL_SCHEME_RE,
OUTPUT_OPTIONS_DEFAULT_OFFLINE,
)
@@ -84,6 +85,7 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
)
# Arguments processing and environment setup.
self._apply_no_options(no_options)
self._process_request_content_type()
self._process_download_options()
self._setup_standard_streams()
self._process_output_options()
@@ -97,12 +99,21 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
self._process_auth()
return self.args
def _process_request_content_type(self):
rct = self.args.request_content_type
self.args.json = rct is RequestContentType.JSON
self.args.multipart = rct is RequestContentType.MULTIPART
self.args.form = rct in {
RequestContentType.FORM,
RequestContentType.MULTIPART,
}
def _process_url(self):
if not URL_SCHEME_RE.match(self.args.url):
if os.path.basename(self.env.program_name) == 'https':
scheme = 'https://'
else:
scheme = self.args.default_scheme + "://"
scheme = self.args.default_scheme + '://'
# See if we're using curl style shorthand for localhost (:3000/foo)
shorthand = re.match(r'^:(?!:)(\d*)(/?.*)$', self.args.url)

View File

@@ -1,6 +1,7 @@
"""Parsing and processing of CLI input (args, auth credentials, files, stdin).
"""
import enum
import re
@@ -10,6 +11,9 @@ import re
# ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
# <https://tools.ietf.org/html/rfc3986#section-3.1>
from enum import Enum
URL_SCHEME_RE = re.compile(r'^[a-z][a-z0-9.+-]*://', re.IGNORECASE)
HTTP_POST = 'POST'
@@ -102,3 +106,9 @@ UNSORTED_FORMAT_OPTIONS_STRING = ','.join(
OUTPUT_OPTIONS_DEFAULT = OUT_RESP_HEAD + OUT_RESP_BODY
OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED = OUT_RESP_BODY
OUTPUT_OPTIONS_DEFAULT_OFFLINE = OUT_REQ_HEAD + OUT_REQ_BODY
class RequestContentType(enum.Enum):
FORM = enum.auto()
MULTIPART = enum.auto()
JSON = enum.auto()

View File

@@ -15,7 +15,8 @@ from httpie.cli.constants import (
DEFAULT_FORMAT_OPTIONS, OUTPUT_OPTIONS,
OUTPUT_OPTIONS_DEFAULT, OUT_REQ_BODY, OUT_REQ_HEAD,
OUT_RESP_BODY, OUT_RESP_HEAD, PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY,
SEPARATOR_GROUP_ALL_ITEMS, SEPARATOR_PROXY, SORTED_FORMAT_OPTIONS_STRING,
RequestContentType, SEPARATOR_GROUP_ALL_ITEMS, SEPARATOR_PROXY,
SORTED_FORMAT_OPTIONS_STRING,
UNSORTED_FORMAT_OPTIONS_STRING,
)
from httpie.output.formatters.colors import (
@@ -141,7 +142,9 @@ content_type = parser.add_argument_group(
content_type.add_argument(
'--json', '-j',
action='store_true',
action='store_const',
const=RequestContentType.JSON,
dest='request_content_type',
help='''
(default) Data items from the command line are serialized as a JSON object.
The Content-Type and Accept headers are set to application/json
@@ -151,7 +154,9 @@ content_type.add_argument(
)
content_type.add_argument(
'--form', '-f',
action='store_true',
action='store_const',
const=RequestContentType.FORM,
dest='request_content_type',
help='''
Data items from the command line are serialized as form fields.
@@ -163,11 +168,12 @@ content_type.add_argument(
)
content_type.add_argument(
'--multipart',
default=False,
action='store_true',
action='store_const',
const=RequestContentType.MULTIPART,
dest='request_content_type',
help='''
Force the request to be encoded as multipart/form-data even without
any file fields. Only has effect only together with --form.
Similar to --form, but always sends a multipart/form-data
request (i.e., even without files).
'''
)