jawah / niquests

β€œSafest, Fastest, Easiest, and Most advanced” Python HTTP Client. Production Ready! Drop-in replacement for Requests. HTTP/1.1, HTTP/2, and HTTP/3 supported.
https://niquests.readthedocs.io/en/latest/
Apache License 2.0
773 stars 18 forks source link
async asyncio certificate-revocation dns-over-https dns-over-quic dns-over-tls dnssec doh doq happy-eyeballs http http-client http2 http3 multiplexed ocsp python quic requests
Niquests Logo

Niquests is a simple, yet elegant, HTTP library. It is a drop-in replacement for Requests, which is under feature freeze.

Niquests, is the β€œSafest, Fastest[^10], Easiest, and Most advanced” Python HTTP Client. Production Ready!

βœ”οΈ Try before you switch: See Multiplexed in Action
πŸ“– See why you should switch: Read about 10 reasons why, and "Revived the promise made six years ago for Requests 3"

πŸ‘† Look at the feature table comparison against requests, httpx and aiohttp! | Feature | niquests | requests | httpx | aiohttp | |--------------------------------------------------------------------------|:-------------------------:|:----------------------------------:|:-----------------------------:|----------------------| | `HTTP/1.1` | βœ… | βœ… | βœ… | βœ… | | `HTTP/2` | βœ… | ❌ | βœ…[^7] | ❌ | | `HTTP/3 over QUIC` | βœ… | ❌ | ❌ | ❌ | | `Synchronous` | βœ… | βœ… | βœ… | ❌ | | `Asynchronous` | βœ… | ❌ | βœ… | βœ… | | `Thread Safe` | βœ… | βœ… | ❌[^5] | _N/A_[^1] | | `Task Safe` | βœ… | _N/A_[^2] | βœ… | βœ… | | `OS Trust Store` | βœ… | ❌ | ❌ | ❌ | | `Multiplexing` | βœ… | ❌ | _Limited_[^3] | ❌ | | `DNSSEC` | βœ…[^11] | ❌ | ❌ | ❌ | | `Customizable DNS Resolution` | βœ… | ❌ | ❌ | βœ… | | `DNS over HTTPS` | βœ… | ❌ | ❌ | ❌ | | `DNS over QUIC` | βœ… | ❌ | ❌ | ❌ | | `DNS over TLS` | βœ… | ❌ | ❌ | ❌ | | `Multiple DNS Resolver` | βœ… | ❌ | ❌ | ❌ | | `Network Fine Tuning & Inspect` | βœ… | ❌ | _Limited_[^6] | _Limited_[^6] | | `Certificate Revocation Protection` | βœ… | ❌ | ❌ | ❌ | | `Session Persistence` | βœ… | βœ… | βœ… | βœ… | | `In-memory Certificate CA & mTLS` | βœ… | ❌ | _Limited_[^4] | _Limited_[^4] | | `SOCKS 4/5 Proxies` | βœ… | βœ… | βœ… | ❌ | | `HTTP/HTTPS Proxies` | βœ… | βœ… | βœ… | βœ… | | `TLS-in-TLS Support` | βœ… | βœ… | βœ… | βœ… | | `Direct HTTP/3 Negotiation` | βœ…[^9] | N/A[^8] | N/A[^8] | N/A[^8] | | `Happy Eyeballs` | βœ… | ❌ | ❌ | βœ… | | `Package / SLSA Signed` | βœ… | ❌ | ❌ | βœ… | | `HTTP/2 with prior knowledge (h2c)` | βœ… | ❌ | βœ… | ❌ |
πŸ“ˆ Look at the performance comparison against them! _Scenario:_ Fetch a thousand requests using 10 tasks or threads, each with a hundred requests using a single pool of connection. **High-Level APIs** | Client | Average Delay to Complete | Notes | |----------|---------------------------|------------------------------| | requests | 987 ms or ~1013 req/s | ThreadPoolExecutor. HTTP/1.1 | | httpx | 720 ms or ~1389 req/s | Asyncio. HTTP/2 | | niquests | 340 ms or ~2941 req/s | Asyncio. HTTP/2 | **Simplified APIs** | Client | Average Delay to Complete | Notes | |---------------|---------------------------|------------------------------| | requests core | 643 ms or ~1555 req/s | ThreadPoolExecutor. HTTP/1.1 | | httpx core | 530 ms or ~1886 req/s | Asyncio. HTTP/2 | | aiohttp | 210 ms or ~4762 req/s | Asyncio. HTTP/1.1 | | niquests core | 170 ms or ~5882 req/s | Asyncio. HTTP/2 | Did you give up on HTTP/2 due to performance concerns? Think again! Do you realize that you can get 3 times faster with the same CPU if you ever switched to Niquests from Requests? Multiplexing and response lazyness open up a wide range of possibilities! Want to learn more about the tests? scripts? reasoning? Take a deeper look at https://github.com/Ousret/niquests-stats ⚠️ Do the responsible thing with this library and do not attempt DoS remote servers using its abilities.
>>> import niquests
>>> s = niquests.Session(resolver="doh+google://", multiplexed=True)
>>> r = s.get('https://pie.dev/basic-auth/user/pass', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.oheaders.content_type.charset
'utf8'
>>> r.encoding
'utf-8'
>>> r.text
'{"authenticated": true, ...'
>>> r.json()
{'authenticated': True, ...}
>>> r
<Response HTTP/3 [200]>
>>> r.ocsp_verified
True
>>> r.conn_info.established_latency
datetime.timedelta(microseconds=38)

or using async/await!

import niquests
import asyncio

async def main() -> None:
    async with niquests.AsyncSession(resolver="doh+google://") as s:
        r = await s.get('https://pie.dev/basic-auth/user/pass', auth=('user', 'pass'), stream=True)
        print(r)  # Output: <Response HTTP/3 [200]>
        payload = await r.json()
        print(payload)  # Output: {'authenticated': True, ...}

asyncio.run(main())

Niquests allows you to send HTTP requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your PUT & POST data β€” just use the json method!

Downloads Supported Versions

This project does not require any compilation toolchain. The HTTP/3 support is not enforced and installed if your platform can support it natively (e.g. pre-built wheel available).

✨ Installing Niquests and Supported Versions

Niquests is available on PyPI:

$ python -m pip install niquests

Niquests officially supports Python or PyPy 3.7+.

πŸš€ Supported Features & Best–Practices

Niquests is ready for the demands of building scalable, robust and reliable HTTP–speaking applications.

Need something more? Create an issue, we listen.

πŸ“ Why did we pursue this?

For many years now, Requests has been frozen. Being left in a vegetative state and not evolving, this blocked millions of developers from using more advanced features.

We don't have to reinvent the wheel all over again, HTTP client Requests is well established and really pleasant in its usage. We believe that Requests has the most inclusive and developer friendly interfaces. We intend to keep it that way. As long as we can, long live Niquests!

How about a nice refresher with a mere CTRL+H import requests to import niquests as requests ?

πŸ’Ό For Enterprise

Professional support for Niquests is available as part of the Tidelift Subscription. Tidelift gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools.

You may also be interested in unlocking specific advantages (like access to a private issue tracker) by looking at our GitHub sponsor tiers.


Niquests is a highly improved HTTP client that is based (forked) on Requests. The previous project original author is Kenneth Reitz and actually left the maintenance of Requests years ago.

[^1]: aiohttp has no support for synchronous request. [^2]: requests has no support for asynchronous request. [^3]: while the HTTP/2 connection object can handle concurrent requests, you cannot leverage its true potential. [^4]: loading client certificate without file can't be done. [^5]: httpx officially claim to be thread safe but recent tests demonstrate otherwise as of february 2024. https://github.com/jawah/niquests/issues/83#issuecomment-1956065258 https://github.com/encode/httpx/issues/3072 https://github.com/encode/httpx/issues/3002 [^6]: they do not expose anything to control network aspects such as IPv4/IPv6 toggles, and timings (e.g. DNS response time, established delay, TLS handshake delay, etc...) and such. [^7]: while advertised as possible, they refuse to make it the default due to performance issues. as of february 2024 an extra is required to enable it manually. [^8]: they don't support HTTP/3 at all. [^9]: you must use a custom DNS resolver so that it can preemptively connect using HTTP/3 over QUIC when remote is compatible. [^10]: performance measured when leveraging a multiplexed connection with or without uses of any form of concurrency as of July 2024. The research compared httpx, requests, aiohttp against niquests. See https://github.com/Ousret/niquests-stats [^11]: enabled when using a custom DNS resolver.