From 56f498c1531fc6cfb277911c67ac436e13752286 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sun, 28 Feb 2016 15:45:45 +0800 Subject: [PATCH] Detect Content Type of file uploaded in multipart/form-data request Closes #271 #285 #398 This adds filename-based detection. It's still not possible to specify the content type manually, though. --- .gitignore | 1 + CHANGELOG.rst | 3 ++- httpie/input.py | 9 +++++++-- tests/test_uploads.py | 2 ++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index c5856256..3bdb64f3 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ README.html htmlcov .idea .DS_Store +.cache/ diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 856364d4..006a35c5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,7 +8,8 @@ This project adheres to `Semantic Versioning `_. `1.0.0-dev`_ (Unreleased) ------------------------- -* Changed the default color style back to ``solarized`` as it supports +* Added ``Content-Type`` of files uploaded in ``multipart/form-data`` requests. +* Changed the default color style back to``solarized`` as it supports both the light and dark terminal background mode. diff --git a/httpie/input.py b/httpie/input.py index b0fbd772..6e259bbe 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -605,9 +605,13 @@ class DataDict(RequestItemsDict): RequestItems = namedtuple('RequestItems', ['headers', 'data', 'files', 'params']) + def get_content_type(filename): - """Get content type for a filename in format appropriate for Content-Type - headers. + """ + Return the content type for ``filename`` in format appropriate + for Content-Type headers, or ``None`` if the file type is unknown + to ``mimetypes``. + """ mime, encoding = mimetypes.guess_type(filename, strict=False) if mime: @@ -616,6 +620,7 @@ def get_content_type(filename): content_type = '%s; charset=%s' % (mime, encoding) return content_type + def parse_items(items, headers_class=CaseInsensitiveDict, data_class=OrderedDict, diff --git a/tests/test_uploads.py b/tests/test_uploads.py index 0bf8457e..5cbe2cab 100644 --- a/tests/test_uploads.py +++ b/tests/test_uploads.py @@ -23,6 +23,7 @@ class TestMultipartFormDataFileUpload: ' filename="%s"' % os.path.basename(FILE_PATH) in r assert FILE_CONTENT in r assert '"foo": "bar"' in r + assert 'Content-Type: text/plain' in r def test_upload_multiple_fields_with_the_same_name(self, httpbin): r = http('--form', '--verbose', 'POST', httpbin.url + '/post', @@ -34,6 +35,7 @@ class TestMultipartFormDataFileUpload: # Should be 4, but is 3 because httpbin # doesn't seem to support filed field lists assert r.count(FILE_CONTENT) in [3, 4] + assert r.count('Content-Type: text/plain') == 2 class TestRequestBodyFromFilePath: