1
0
mirror of https://github.com/httpie/cli.git synced 2026-06-20 11:32:56 +02:00

Allow multiple fields with the same name.

Applies to form data and URL params:

    http -f url a=1 a=2
    http url a==1 a==2
This commit is contained in:
Jakub Roztocil
2012-07-24 16:46:04 +02:00
parent 9944def703
commit 7af08b6faa
5 changed files with 68 additions and 20 deletions
+1 -1
View File
@@ -155,7 +155,7 @@ parser.add_argument(
)
parser.add_argument(
'--auth-type', choices=['basic', 'digest'],
'--auth-type', choices=['basic', 'digest'], default='basic',
help=_('''
The authentication mechanism to be used.
Defaults to "basic".
+25 -8
View File
@@ -56,7 +56,6 @@ class Parser(argparse.ArgumentParser):
args = super(Parser, self).parse_args(args, namespace)
self._process_output_options(args, env)
self._validate_auth_options(args)
self._guess_method(args, env)
self._parse_items(args)
@@ -124,9 +123,9 @@ class Parser(argparse.ArgumentParser):
"""
args.headers = CaseInsensitiveDict()
args.headers['User-Agent'] = DEFAULT_UA
args.data = OrderedDict()
args.data = ParamDict() if args.form else OrderedDict()
args.files = OrderedDict()
args.params = OrderedDict()
args.params = ParamDict()
try:
parse_items(items=args.items,
headers=args.headers,
@@ -173,10 +172,6 @@ class Parser(argparse.ArgumentParser):
','.join(unknown)
)
def _validate_auth_options(self, args):
if args.auth_type and not args.auth:
self.error('--auth-type can only be used with --auth')
class ParseError(Exception):
pass
@@ -319,6 +314,28 @@ class AuthCredentialsArgType(KeyValueArgType):
)
class ParamDict(OrderedDict):
def __setitem__(self, key, value):
"""
If `key` is assigned more than once, `self[key]` holds a
`list` of all the values.
This allows having multiple fields with the same name in form
data and URL params.
"""
# NOTE: Won't work when used for form data with multiple values
# for a field and a file field is present:
# https://github.com/kennethreitz/requests/issues/737
if key not in self:
super(ParamDict, self).__setitem__(key, value)
else:
if not isinstance(self[key], list):
super(ParamDict, self).__setitem__(key, [self[key]])
self[key].append(value)
def parse_items(items, data=None, headers=None, files=None, params=None):
"""
Parse `KeyValue` `items` into `data`, `headers`, `files`,
@@ -332,7 +349,7 @@ def parse_items(items, data=None, headers=None, files=None, params=None):
if files is None:
files = {}
if params is None:
params = {}
params = ParamDict()
for item in items:
value = item.value
key = item.key
+4 -4
View File
@@ -37,10 +37,10 @@ def get_response(args):
try:
credentials = None
if args.auth:
auth_type = (requests.auth.HTTPDigestAuth
if args.auth_type == 'digest'
else requests.auth.HTTPBasicAuth)
credentials = auth_type(args.auth.key, args.auth.value)
credentials = {
'basic': requests.auth.HTTPBasicAuth,
'digest': requests.auth.HTTPDigestAuth,
}[args.auth_type](args.auth.key, args.auth.value)
return requests.request(
method=args.method.lower(),