Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File upload support #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/user-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ The following are all valid:
> names:=["foo","bar"]
> user:='{"username": "foo", "password": "bar"}'

# File upload parameters needs to have --form enabled
> number@~/test.txt
> [email protected]

# Write them in one line
> Content-Type:application/json page==2 username=foo

Expand Down
4 changes: 4 additions & 0 deletions http_prompt/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def __init__(self, url=None, spec=None):
self.headers = {}
self.querystring_params = {}
self.body_params = {}
self.file_params = {}
self.body_json_params = {}
self.options = {}
self.should_exit = False
Expand Down Expand Up @@ -52,6 +53,7 @@ def __eq__(self, other):
self.options == other.options and
self.querystring_params == other.querystring_params and
self.body_params == other.body_params and
self.file_params == other.file_params and
self.body_json_params == other.body_json_params and
self.should_exit == other.should_exit)

Expand All @@ -60,6 +62,7 @@ def copy(self):
context.headers = self.headers.copy()
context.querystring_params = self.querystring_params.copy()
context.body_params = self.body_params.copy()
context.file_params = self.file_params.copy()
context.body_json_params = self.body_json_params.copy()
context.options = self.options.copy()
context.should_exit = self.should_exit
Expand All @@ -72,6 +75,7 @@ def update(self, context):
self.headers.update(context.headers)
self.querystring_params.update(context.querystring_params)
self.body_params.update(context.body_params)
self.file_params.update(context.file_params)
self.body_json_params.update(context.body_json_params)
self.options.update(context.options)
self.should_exit = self.should_exit
2 changes: 2 additions & 0 deletions http_prompt/context/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def _extract_httpie_request_items(context, quote=False):
('==', context.querystring_params),
(':=', context.body_json_params),
('=', context.body_params),
('@', context.file_params),
(':', context.headers)

]
for sep, item_dict in operators_and_items:
for k, value in sorted(six.iteritems(item_dict)):
Expand Down
24 changes: 15 additions & 9 deletions http_prompt/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@
full_dquoted_mut = _ '"' dquoted_mutkey mutop dquoted_mutval '"' _
value_squoted_mut = _ unquoted_mutkey mutop "'" squoted_mutval "'" _
value_dquoted_mut = _ unquoted_mutkey mutop '"' dquoted_mutval '"' _
mutop = ":=" / ":" / "==" / "="
mutop = ":=" / ":" / "==" / "=" / "@"
unquoted_mutkey = unquoted_mutkey_item+
unquoted_mutval = unquoted_stringitem*
unquoted_mutkey_item = shell_subs / unquoted_mutkey_char / escapeseq
unquoted_mutkey_char = ~r"[^\s'\"\\=:>]"
unquoted_mutkey_char = ~r"[^\s'\"\\=:>@]"
squoted_mutkey = squoted_mutkey_item+
squoted_mutval = squoted_stringitem*
squoted_mutkey_item = shell_subs / squoted_mutkey_char / escapeseq
squoted_mutkey_char = ~r"[^\r\n'\\=:]"
squoted_mutkey_char = ~r"[^\r\n'\\=:@]"
dquoted_mutkey = dquoted_mutkey_item+
dquoted_mutval = dquoted_stringitem*
dquoted_mutkey_item = shell_subs / dquoted_mutkey_char / escapeseq
dquoted_mutkey_char = ~r'[^\r\n"\\=:]'
dquoted_mutkey_char = ~r'[^\r\n"\\=:@]'

option_mut = flag_option_mut / value_option_mut
flag_option_mut = _ flag_optname _
Expand Down Expand Up @@ -255,6 +255,7 @@ def visit_rm(self, node, children):
for target in [self.context.headers,
self.context.querystring_params,
self.context.body_params,
self.context.file_params,
self.context.body_json_params,
self.context.options]:
target.clear()
Expand All @@ -272,13 +273,16 @@ def visit_rm(self, node, children):
# TODO: This is kind of ugly, will fix it
if name == '*':
self.context.body_params.clear()
self.context.file_params.clear()
self.context.body_json_params.clear()
target = {}
else:
try:
del self.context.body_params[name]
except KeyError:
del self.context.body_json_params[name]
return node
if name in self.context.body_params:
target = self.context.body_params
elif name in self.context.file_params:
target = self.context.file_params
elif name in self.context.body_json_params:
target = self.context.body_json_params

if name == '*':
target.clear()
Expand Down Expand Up @@ -375,6 +379,8 @@ def _mutate(self, node, key, op, val):
self.context_override.headers[key] = val
elif op == '=':
self.context_override.body_params[key] = val
elif op == '@':
self.context_override.file_params[key] = val
elif op == '==':
# You can have multiple querystring params with the same name,
# so we use a list to store multiple values (#20)
Expand Down
6 changes: 3 additions & 3 deletions http_prompt/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ class HttpPromptLexer(RegexLexer):

# Unquoted or value-quoted request mutation,
# such as (name="John Doe") and (name=John\ Doe)
(r'((?:[^\s\'"\\=:]|(?:\\.))*)(:=|:|==|=)',
(r'((?:[^\s\'"\\=:]|(?:\\.))*)(:=|:|==|=|@)',
bygroups(Name, Operator),
combined('shell_command', 'unquoted_mut')),

# Full single-quoted request mutation, such as ('name=John Doe')
(r"(')((?:[^\r\n'\\=:]|(?:\\.))+)(:=|:|==|=)",
(r"(')((?:[^\r\n'\\=:]|(?:\\.))+)(:=|:|==|=|@)",
bygroups(Text, Name, Operator),
combined('shell_command', 'squoted_mut')),

# Full double-quoted request mutation, such as ("name=John Doe")
(r'(")((?:[^\r\n"\\=:]|(?:\\.))+)(:=|:|==|=)',
(r'(")((?:[^\r\n"\\=:]|(?:\\.))+)(:=|:|==|=|@)',
bygroups(Text, Name, Operator),
combined('shell_command', 'dquoted_mut'))
],
Expand Down