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

Added file upload support

It is now possible to send multipart/form-data requests.

Note that the --file option used previously has been removed
because it didn't allow you specify the field name.

Example:

    http -f POST example.com field-name@/path/to/file
This commit is contained in:
Jakub Roztocil
2012-03-14 12:17:39 +01:00
parent 578acacdf3
commit b7e0473d6c
6 changed files with 95 additions and 45 deletions

View File

@@ -100,26 +100,37 @@ def main(args=None,
headers = CaseInsensitiveDict()
headers['User-Agent'] = DEFAULT_UA
data = OrderedDict()
files = OrderedDict()
try:
cli.parse_items(items=args.items, headers=headers, data=data)
cli.parse_items(items=args.items, headers=headers,
data=data, files=files)
except cli.ParseError as e:
if args.traceback:
raise
parser.error(e.message)
if files and not args.form:
# We could just switch to --form automatically here,
# but I think it's better to make it explicit.
parser.error(
' You need to set the --form / -f flag to'
' to issue a multipart request. File fields: %s'
% ','.join(files.keys()))
if not stdin_isatty:
if data:
parser.error('Request body (stdin) and request '
'data (key=value) cannot be mixed.')
data = stdin.read()
# JSON/Form content type.
if args.json or (not args.form and data):
if stdin_isatty:
data = json.dumps(data)
if 'Content-Type' not in headers and (data or args.json):
if not files and ('Content-Type' not in headers and (data or args.json)):
headers['Content-Type'] = TYPE_JSON
elif 'Content-Type' not in headers:
elif not files and 'Content-Type' not in headers:
headers['Content-Type'] = TYPE_FORM
# Fire the request.
@@ -133,7 +144,7 @@ def main(args=None,
timeout=args.timeout,
auth=(args.auth.key, args.auth.value) if args.auth else None,
proxies=dict((p.key, p.value) for p in args.proxy),
files=dict((os.path.basename(f.name), f) for f in args.file),
files=files,
allow_redirects=args.allow_redirects,
)
except (KeyboardInterrupt, SystemExit):