FastAPI request forwarding middleware
Project description
FastAPI Forward
FastAPI Forward is a middleware that enables request forwarding/proxying in FastAPI applications. It allows you to easily forward specific routes to different target URLs while maintaining the original request context.
Features
- Simple decorator-based route forwarding
- Preserves request headers and body
- Supports conditional forwarding
- Easy integration with FastAPI applications
Installation
pip install fastapi-forward
Quick Start
Here's a simple example that forwards requests from your FastAPI application to another service:
from fastapi import FastAPI
from fastapi_forward import forward, init_app
app = FastAPI()
init_app(app)
@app.get("/")
@forward("https://api.example.com")
async def root():
return {"message": "This response will be replaced by the forwarded response"}
In this example, any GET request to "/" will be forwarded to "https://api.example.com/".
Usage
Basic Forwarding
The most basic usage is to forward a route to another URL:
@app.get("/users")
@forward("https://api.example.com")
async def get_users():
pass
All requests to "/users" will be forwarded to "https://api.example.com/users".
Initialization
Make sure to initialize the middleware before using the forward decorator:
from fastapi import FastAPI
from fastapi_forward import init_app
app = FastAPI()
init_app(app)
Request Handling
The middleware:
- Preserves request headers
- Forwards request body
- Maintains HTTP methods
- Returns the response from the target URL
Requirements
- Python 3.10
- FastAPI
- httpx >= 0.28.0
Development
To set up the development environment:
# Install PDM if you haven't already
pip install pdm
# Install dependencies
pdm install
# Run tests
pdm run pytest
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_forward-0.0.1.tar.gz.
File metadata
- Download URL: fastapi_forward-0.0.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.21.0 CPython/3.10.15 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b9434a7f7ef19fde8e45ae3a2384bb531fd7f9d05c0654b3fdf2154bb752278
|
|
| MD5 |
0ce2dcf538e3e62a4daec3f3b879a6cf
|
|
| BLAKE2b-256 |
b28157e06840b8ee3d43814e2a4b77fee7e5a893361de005d27392658ec32428
|
File details
Details for the file fastapi_forward-0.0.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_forward-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.21.0 CPython/3.10.15 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb8a2308592e7877880c0e24511487d0ff91ebfb23866147c4d261aa7bfefeea
|
|
| MD5 |
60d7d4f62db8ae18e90a7766702707dd
|
|
| BLAKE2b-256 |
aca70779c4df6166587a1ec7d9a91613121332e3295594e1819569995438355a
|