Skip to content

Commit

Permalink
Merge 328ece6 into 0f4dce9
Browse files Browse the repository at this point in the history
  • Loading branch information
dair-targ committed Mar 2, 2018
2 parents 0f4dce9 + 328ece6 commit 6978aeb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
9 changes: 7 additions & 2 deletions README.rst
Expand Up @@ -368,7 +368,7 @@ form data, files, and URL parameters.
They are key/value pairs specified after the URL. All have in
common that they become part of the actual request that is sent and that
their type is distinguished only by the separator used:
``:``, ``=``, ``:=``, ``==``, ``@``, ``=@``, and ``:=@``. The ones with an
``:``, ``=``, ``:=``, ``==``, ``@``, ``=@``, ``:=@``, and ``==@``. The ones with an
``@`` expect a file path as value.
+-----------------------+-----------------------------------------------------+
Expand Down Expand Up @@ -396,7 +396,12 @@ their type is distinguished only by the separator used:
| | The presence of a file field results |
| | in a ``multipart/form-data`` request. |
+-----------------------+-----------------------------------------------------+
| URL parameters | Reads the content of the ``file.txt``, |
| from file | removes all trailing new lines characters |
| ``name==@file.txt`` | and appends the name/content pair as a query |
| | string parameter to the URL. |
| | The ``==@`` separator is used. |
+-----------------------+-----------------------------------------------------+
Note that data fields aren't the only way to specify request data:
`Redirected input`_ is a mechanism for passing arbitrary request data.
Expand Down
31 changes: 20 additions & 11 deletions httpie/input.py
Expand Up @@ -42,6 +42,7 @@
SEP_DATA_EMBED_FILE = '=@'
SEP_DATA_EMBED_RAW_JSON_FILE = ':=@'
SEP_QUERY = '=='
SEP_QUERY_EMBED_FILE = '==@'

# Separators that become request data
SEP_GROUP_DATA_ITEMS = frozenset([
Expand Down Expand Up @@ -69,6 +70,7 @@
SEP_HEADERS,
SEP_HEADERS_EMPTY,
SEP_QUERY,
SEP_QUERY_EMBED_FILE,
SEP_DATA,
SEP_DATA_RAW_JSON,
SEP_FILES,
Expand Down Expand Up @@ -675,6 +677,20 @@ def get_content_type(filename):
return content_type


def read_unicode_file(item, filename):
try:
with open(os.path.expanduser(filename), 'rb') as f:
return f.read().decode('utf8')
except IOError as e:
raise ParseError('"%s": %s' % (item.orig, e))
except UnicodeDecodeError:
raise ParseError(
'"%s": cannot embed the content of "%s",'
' not a UTF8 or ASCII-encoded text file'
% (item.orig, item.value)
)


def parse_items(items,
headers_class=CaseInsensitiveDict,
data_class=OrderedDict,
Expand Down Expand Up @@ -705,6 +721,9 @@ def parse_items(items,
target = headers
elif item.sep == SEP_QUERY:
target = params
elif item.sep == SEP_QUERY_EMBED_FILE:
value = read_unicode_file(item, value).rstrip('\n')
target = params
elif item.sep == SEP_FILES:
try:
with open(os.path.expanduser(value), 'rb') as f:
Expand All @@ -718,17 +737,7 @@ def parse_items(items,
elif item.sep in SEP_GROUP_DATA_ITEMS:

if item.sep in SEP_GROUP_DATA_EMBED_ITEMS:
try:
with open(os.path.expanduser(value), 'rb') as f:
value = f.read().decode('utf8')
except IOError as e:
raise ParseError('"%s": %s' % (item.orig, e))
except UnicodeDecodeError:
raise ParseError(
'"%s": cannot embed the content of "%s",'
' not a UTF8 or ASCII-encoded text file'
% (item.orig, item.value)
)
value = read_unicode_file(item, value)

if item.sep in SEP_GROUP_RAW_JSON_ITEMS:
try:
Expand Down
6 changes: 5 additions & 1 deletion tests/test_cli.py
Expand Up @@ -77,6 +77,7 @@ def test_valid_items(self):
self.key_value('bool:=true'),
self.key_value('file@' + FILE_PATH_ARG),
self.key_value('query==value'),
self.key_value('query-embed==@' + FILE_PATH_ARG),
self.key_value('string-embed=@' + FILE_PATH_ARG),
self.key_value('raw-json-embed:=@' + JSON_FILE_PATH_ARG),
])
Expand Down Expand Up @@ -104,7 +105,10 @@ def test_valid_items(self):
}

# Parsed query string parameters
assert items.params == {'query': 'value'}
assert items.params == {
'query': 'value',
'query-embed': FILE_CONTENT,
}

# Parsed file fields
assert 'file' in items.files
Expand Down

0 comments on commit 6978aeb

Please sign in to comment.