Convert Figma logo to code with AI

psf logorequests

A simple, yet elegant, HTTP library.

51,964
9,286
51,964
243

Top Related Projects

14,938

Asynchronous HTTP client/server framework for asyncio and Python

3,759

urllib3 is a user-friendly HTTP client library for Python

12,973

A next generation HTTP client for Python. 🦋

Requests + Gevent = <3

12,679

HTTP Request & Response Service, written in Python + Flask.

Quick Overview

Requests is a popular HTTP library for Python, designed to be simple and human-friendly. It abstracts the complexities of making HTTP requests, allowing developers to easily send HTTP/1.1 requests without manual labor.

Pros

  • Simple and intuitive API
  • Automatic decompression and decoding of response content
  • Built-in session persistence and cookie handling
  • Extensive documentation and community support

Cons

  • Not suitable for high-performance, high-concurrency scenarios
  • Lacks built-in async support (though there are async alternatives)
  • May be overkill for very simple use cases
  • No built-in retry mechanism for failed requests

Code Examples

  1. Making a simple GET request:
import requests

response = requests.get('https://api.github.com')
print(response.status_code)
print(response.json())
  1. Sending POST data:
import requests

data = {'key': 'value'}
response = requests.post('https://httpbin.org/post', data=data)
print(response.text)
  1. Using session objects for persistent cookies:
import requests

session = requests.Session()
session.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
response = session.get('https://httpbin.org/cookies')
print(response.text)

Getting Started

To get started with Requests, first install it using pip:

pip install requests

Then, you can start using it in your Python scripts:

import requests

# Make a GET request
response = requests.get('https://api.github.com')

# Check the status code
if response.status_code == 200:
    # Print the response content
    print(response.json())
else:
    print(f"Request failed with status code: {response.status_code}")

This simple example demonstrates how to make a GET request to the GitHub API, check the status code, and print the JSON response if the request was successful.

Competitor Comparisons

14,938

Asynchronous HTTP client/server framework for asyncio and Python

Pros of aiohttp

  • Asynchronous: Supports non-blocking I/O operations
  • WebSocket support: Built-in functionality for WebSocket connections
  • Server-side capabilities: Can be used to create both clients and servers

Cons of aiohttp

  • Steeper learning curve: Requires understanding of async/await concepts
  • Less widespread adoption: Smaller community and fewer third-party extensions
  • More complex API: May require more code for simple tasks compared to requests

Code Comparison

requests:

import requests

response = requests.get('https://api.example.com/data')
print(response.json())

aiohttp:

import aiohttp
import asyncio

async def fetch_data():
    async with aiohttp.ClientSession() as session:
        async with session.get('https://api.example.com/data') as response:
            return await response.json()

data = asyncio.run(fetch_data())
print(data)

Both requests and aiohttp are popular Python libraries for making HTTP requests. requests is known for its simplicity and ease of use, making it an excellent choice for synchronous applications. aiohttp, on the other hand, is designed for asynchronous programming and offers more advanced features like WebSocket support and server-side capabilities. While aiohttp provides better performance for concurrent operations, it comes with a steeper learning curve and a more complex API. The choice between the two depends on the specific requirements of your project and your familiarity with asynchronous programming concepts.

3,759

urllib3 is a user-friendly HTTP client library for Python

Pros of urllib3

  • More fine-grained control over connection pooling and reuse
  • Supports thread-safe connection pooling
  • Lighter weight and faster for certain use cases

Cons of urllib3

  • Less user-friendly API compared to Requests
  • Fewer high-level abstractions for common tasks
  • Requires more manual configuration for advanced features

Code Comparison

urllib3:

import urllib3

http = urllib3.PoolManager()
r = http.request('GET', 'http://example.com')
print(r.status)
print(r.data)

Requests:

import requests

r = requests.get('http://example.com')
print(r.status_code)
print(r.text)

Both urllib3 and Requests are popular Python libraries for making HTTP requests. urllib3 offers more low-level control and is the foundation for Requests, while Requests provides a more user-friendly interface with additional features.

urllib3 excels in scenarios requiring fine-tuned connection management and pooling, making it suitable for advanced use cases. Requests, on the other hand, simplifies common HTTP operations and offers a more intuitive API for most developers.

The code comparison demonstrates the difference in API design. urllib3 requires more setup but provides direct access to connection pooling, while Requests offers a more concise and readable syntax for basic HTTP requests.

12,973

A next generation HTTP client for Python. 🦋

Pros of httpx

  • Supports both sync and async HTTP requests
  • Built-in support for HTTP/2
  • More modern API design with type hints

Cons of httpx

  • Smaller ecosystem and fewer third-party extensions
  • Less mature and battle-tested compared to requests
  • Slightly steeper learning curve for developers familiar with requests

Code Comparison

requests:

import requests

response = requests.get('https://api.example.com')
data = response.json()

httpx:

import httpx

with httpx.Client() as client:
    response = client.get('https://api.example.com')
    data = response.json()

Both libraries offer similar basic functionality, but httpx provides more options for advanced use cases, such as async operations:

async with httpx.AsyncClient() as client:
    response = await client.get('https://api.example.com')
    data = response.json()

httpx also supports HTTP/2 out of the box, which can be enabled easily:

client = httpx.Client(http2=True)

While requests has been the go-to library for HTTP in Python for years, httpx offers a more modern alternative with support for newer protocols and async operations. However, requests still has a larger ecosystem and is more widely used, making it a solid choice for many projects.

Requests + Gevent = <3

Pros of grequests

  • Asynchronous HTTP requests, allowing for faster execution of multiple requests
  • Built on top of requests, maintaining familiar API and functionality
  • Simple integration with existing requests-based code

Cons of grequests

  • Less actively maintained compared to requests
  • May introduce additional complexity for simple use cases
  • Potential for race conditions and other concurrency-related issues

Code Comparison

requests:

import requests

urls = ['http://example.com', 'http://example.org', 'http://example.net']
for url in urls:
    response = requests.get(url)
    print(response.status_code)

grequests:

import grequests

urls = ['http://example.com', 'http://example.org', 'http://example.net']
responses = grequests.map(grequests.get(u) for u in urls)
for response in responses:
    print(response.status_code)

The main difference is that grequests allows for concurrent execution of requests, potentially improving performance for multiple HTTP requests. However, requests provides a simpler, synchronous approach that may be more suitable for straightforward use cases or when concurrency is not required.

12,679

HTTP Request & Response Service, written in Python + Flask.

Pros of httpbin

  • Provides a ready-to-use HTTP testing service
  • Offers a wide range of HTTP scenarios for testing
  • Lightweight and easy to deploy locally or in the cloud

Cons of httpbin

  • Limited to testing HTTP interactions only
  • Requires external setup for more complex scenarios
  • Less flexible for custom request handling

Code Comparison

requests:

import requests

response = requests.get('https://api.example.com/data')
data = response.json()

httpbin:

import requests

response = requests.get('https://httpbin.org/get')
data = response.json()

Key Differences

  • requests is a full-featured HTTP client library for Python
  • httpbin is a HTTP request & response service for testing
  • requests is used for making HTTP requests in applications
  • httpbin is primarily used for testing HTTP clients and APIs

Use Cases

  • Use requests when building applications that need to interact with web services
  • Use httpbin when testing HTTP clients, debugging API integrations, or learning about HTTP

Community and Maintenance

  • requests has a larger community and more frequent updates
  • httpbin is maintained by Postman and has a focused use case

Both projects are valuable tools in the HTTP ecosystem, serving different but complementary purposes for developers working with web technologies.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Requests

Requests is a simple, yet elegant, HTTP library.

>>> import requests
>>> r = requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
'{"authenticated": true, ...'
>>> r.json()
{'authenticated': True, ...}

Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your PUT & POST data — but nowadays, just use the json method!

Requests is one of the most downloaded Python packages today, pulling in around 30M downloads / week— according to GitHub, Requests is currently depended upon by 1,000,000+ repositories. You may certainly put your trust in this code.

Downloads Supported Versions Contributors

Installing Requests and Supported Versions

Requests is available on PyPI:

$ python -m pip install requests

Requests officially supports Python 3.8+.

Supported Features & Best–Practices

Requests is ready for the demands of building robust and reliable HTTP–speaking applications, for the needs of today.

  • Keep-Alive & Connection Pooling
  • International Domains and URLs
  • Sessions with Cookie Persistence
  • Browser-style TLS/SSL Verification
  • Basic & Digest Authentication
  • Familiar dict–like Cookies
  • Automatic Content Decompression and Decoding
  • Multi-part File Uploads
  • SOCKS Proxy Support
  • Connection Timeouts
  • Streaming Downloads
  • Automatic honoring of .netrc
  • Chunked HTTP Requests

API Reference and User Guide available on Read the Docs

Read the Docs

Cloning the repository

When cloning the Requests repository, you may need to add the -c fetch.fsck.badTimezone=ignore flag to avoid an error about a bad commit (see this issue for more background):

git clone -c fetch.fsck.badTimezone=ignore https://github.com/psf/requests.git

You can also apply this setting to your global Git config:

git config --global fetch.fsck.badTimezone ignore

Kenneth Reitz Python Software Foundation