question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Python API support

See original GitHub 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:

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

Thoughts?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
jakubroztocilcommented, Apr 10, 2015

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})
0reactions
Almadcommented, Feb 24, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python and REST APIs: Interacting With Web Services
By using Python and REST APIs, you can retrieve, parse, update, and manipulate the data provided by any web service you're interested in....
Read more >
Python API Tutorial: Getting Started with APIs - Dataquest
In this Python API tutorial, learn about APIs by requesting and analyzing data from the international space station using Python.
Read more >
How to use an API with Python (Beginner's Guide) - RapidAPI
After registration, go to the NASA API page. Enter its name in the search box at the RapidAPI service or go to the...
Read more >
Python/C API Reference Manual — Python 3.11.1 ...
This manual documents the API used by C and C++ programmers who want to write extension modules or embed Python. It is a...
Read more >
How To Use Web APIs in Python 3 | DigitalOcean
An API wrapper is code that you install on your system to make the ... the APIs themselves can do will help you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found