Comments (5)
Yes it works! thank you
async def too_many_requests(scope: Scope, receive: Receive, send: Send) -> None:
request = Request(scope=scope)
body_dict = {
'error': {
'status': 429,
'detail': 'Too Many Requests',
'method': request.method,
'path': request.url.path,
'query': dict(request.query_params)
}
}
body = json.dumps(body_dict).encode('utf-8')
await send({"type": "http.response.start", "status": 429, "headers": [(b"content-type", b"application/json")]})
await send({"type": "http.response.body", "body": body})
from asgi-ratelimit.
Good issue! I will add an overridable function to it for custom response.
from asgi-ratelimit.
@abersheeran thank you so much
from asgi-ratelimit.
Currently I override the response like this
@app.middleware("http")
async def custom_response(request, call_next):
response = await call_next(request)
if response.status_code == 429:
return JSONResponse({
"error": {
"status": response.status_code,
"detail": '429 Too Many Requests'
}
}, status_code=response.status_code)
secure_headers.starlette(response)
return response
from asgi-ratelimit.
https://github.com/abersheeran/asgi-ratelimit/tree/v0.4.0#custom-block-handler
Done.
from asgi-ratelimit.
Related Issues (20)
- feature: custom 429 per limit HOT 1
- feature: gcra algorithm HOT 1
- Exclude certain paths? HOT 2
- docs: custom block needs to be adapted HOT 2
- Any way to get apply limit on entire app or a group of endpoints HOT 8
- 需求类issue
- customize user and group key HOT 1
- The wrong ttl
- release v0.6.1 and publish HOT 1
- Ratelimit user_id for JWT access token , IP address for endpoint having not token.
- Please release 0.7.0 HOT 1
- init ratelimiter backend from config, not code
- Zero rating behaviour in MemoryBackend, blocking abusive clients as part of rate limiting HOT 1
- feature: Redis, more params to support ssl connect HOT 2
- Wrong import in example HOT 1
- Add type hint annotations
- Rate limit per request method HOT 12
- New Version Release HOT 2
- Access hit/miss ratio? HOT 1
- `Event loop is closed` when testing with `TestClient` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asgi-ratelimit.