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

Add Python API support #205

Closed
giampaolo opened this issue Feb 27, 2014 · 7 comments
Closed

Add Python API support #205

giampaolo opened this issue Feb 27, 2014 · 7 comments
Labels
duplicate Issue already exists enhancement New feature or enhancement

Comments

@giampaolo
Copy link

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:

>>> from httpie import interpret_args
>>> resp = interpret_args("http GET http://localhost:8000/help")
>>> resp
<Response [200]>
>>> resp.status_code
200
>>> 

Thoughts?

@ticky
Copy link

ticky commented Feb 27, 2014

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
>>> 

@giampaolo
Copy link
Author

I see httpie as a very valuable language abstraction on top of requests.
With a single httpie line you can express concepts which you would otherwise express in 2-3-10 lines of requests/python code.
As such you can use it as an actual mini language, which you can even integrate into configuration files in form of a plain string (this is my use case actually, and this is why I'm here hehe).
Plus, if one is already proficient with httpie syntax it's easier for him to just integrate it in a python script by using an actual API as opposed to launching a subprocess and parsing its retcode and output, which is of course suboptimal.

@giampaolo
Copy link
Author

I'm not sure if my idea fits well with all httpie usage patterns.
It probably works as long as it makes sense to have a Response object in return (instead of something else) which AFAICT is the case most of the times if not all.

@jkbrzt jkbrzt added the feature label Apr 10, 2014
@jkbrzt
Copy link
Member

jkbrzt commented Apr 10, 2014

The interesting part of what HTTPie does in this regard is the translation of a command line argument list to the **kwargs passed to requests.request().

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 --debug flag is set:

$ 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})

@felipevolpone
Copy link

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?

@ryneeverett
Copy link

Here's another use case I ran into in the wild: calling httpie in a subprocess just for the pretty printing and logging.

@Almad Almad changed the title Add API support Add Python API support Feb 24, 2021
@Almad
Copy link

Almad commented Feb 24, 2021

This is a duplicate to #551—let's use that one as a canonical issue.

@Almad Almad closed this as completed Feb 24, 2021
@Almad Almad added the duplicate Issue already exists label Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issue already exists enhancement New feature or enhancement
Projects
None yet
Development

No branches or pull requests

6 participants