-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Add Python API support #205
Comments
Wouldn't you be better off just using Requests, which httpie is based upon? >>> import requests
>>> resp = requests.get("http://localhost:8000/help")
>>> resp
<Response [200]>
>>> resp.status_code
200
>>> |
I see httpie as a very valuable language abstraction on top of requests. |
I'm not sure if my idea fits well with all httpie usage patterns. |
The interesting part of what HTTPie does in this regard is the translation of a command line argument list to the If it was to be exposed, then it would look something like this: import requests
from httpie.cli import parser
from httpie.context import Environment
from httpie.client import get_requests_kwargs
def command_to_requests_kwargs(command):
args = parser.parse_args(args=command.split(), env=Environment())
return get_requests_kwargs(args)
kwargs = command_to_requests_kwargs("""
--auth-type=digest --auth=user:password'
PUT httpbin.org/put
Referer:example.org
User-Agent:Bacon/1.0
""")
>>> kwargs
{'allow_redirects': False,
'auth': <requests.auth.HTTPDigestAuth at 0x10db48ed0>,
'cert': None,
'data': OrderedDict(),
'files': OrderedDict(),
'headers': CaseInsensitiveDict({'Referer': 'example.org', 'User-Agent': 'Bacon/1.0'}),
'method': 'patch',
'params': ParamDict(),
'proxies': {},
'stream': True,
'timeout': 30,
'url': 'http://example.org/foo',
'verify': True}
>>> requests.request(**kwargs)
<Response [405]> HTTPie actually spits out the Python code used to make the request when the $ http --debug --auth-type=digest --auth=user:password PATCH example.org/foo Referer:example.org User-Agent:Bacon/1.0
HTTPie 0.8.0
HTTPie data: /Users/jakub/.httpie
Requests 2.2.1
Pygments 1.6
Python 2.7.6 (default, Nov 21 2013, 11:09:15)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.1.72)] darwin
>>> requests.request({'allow_redirects': False,
'auth': <requests.auth.HTTPDigestAuth object at 0x10c8b8a10>,
'cert': None,
'data': OrderedDict(),
'files': OrderedDict(),
'headers': CaseInsensitiveDict({'Referer': 'example.org', 'User-Agent': 'Bacon/1.0'}),
'method': 'patch',
'params': ParamDict(),
'proxies': {},
'stream': True,
'timeout': 30,
'url': 'http://example.org/foo',
'verify': True}) |
I think that I could start the process to create an abstraction layer to provide httpie as an API. There is some task already in course to do this? |
Here's another use case I ran into in the wild: calling httpie in a subprocess just for the pretty printing and logging. |
This is a duplicate to #551—let's use that one as a canonical issue. |
httpie is a very cool CLI tool, far better than curl.
It would be great if there would a minimal documented API support so that it can also be used as a Python module you can import and make requests with.
The way I would expect it to work might be something like this:
Thoughts?
The text was updated successfully, but these errors were encountered: