-
Notifications
You must be signed in to change notification settings - Fork 4
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
docs: add example of Modify (redefine) response only to particular endpoint
#37
base: main
Are you sure you want to change the base?
Changes from all commits
9a2e086
1d26229
f372bc4
c8533aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,3 +118,35 @@ async def _(request: Request, path: str = ""): | |
``` | ||
|
||
visit `/`, you will notice that the response body is printed to the console. | ||
|
||
|
||
## Modify (redefine) response only to particular endpoint | ||
|
||
```python | ||
from fastapi import FastAPI | ||
from starlette.requests import Request | ||
from fastapi_proxy_lib.core.http import ReverseHttpProxy | ||
|
||
app = FastAPI() | ||
proxy = ReverseHttpProxy(base_url="https://httpbin.org/") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we should use |
||
|
||
@app.get("/{path:path}") | ||
async def _(request: Request, path: str = ""): | ||
if path == "ip" and request.method == "GET": | ||
Comment on lines
+132
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add |
||
return { "msg": "Method is redefined"} | ||
else: | ||
proxy_response = await proxy.proxy(request=request, path=path) | ||
return proxy_response | ||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
uvicorn.run(app, host="0.0.0.0", port=8000) | ||
``` | ||
|
||
In this example all requests except `GET /ip` will be passed to `httpbin.org`: | ||
|
||
```python | ||
python3 test.py # Uvicorn running on http://0.0.0.0:8000 | ||
curl http://127.0.0.0:8000/user-agent # { "user-agent": "curl/7.81.0" } | ||
curl http://127.0.0.0:8000/ip # {"msg":"Method is redefined"} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps adding
lifespan
would be better?