要是 Follow 了我的应该知道我之前 fork 一版 httpbin 改了代码让它能够支持部署在指定前缀下,但无奈这货太坑了,居然还排除一些 Headers 不给返回,自己写了一个 baize 版本的 httpbin ,真正的原封不动的把请求信息还回去。
https://github.com/index-py/httpbin 顺便尝试了一下 heroku 一键部署,十分好用。
另外,还可以使用 baize 的 SubPaths 、Router 、Host 之类的路由组合功能结合到现有的 Web 项目里,不需要额外部署。
from baize.asgi import request_response, Request, JSONResponse
from baize.exceptions import HTTPException
@request_response
async def anything(request: Request) -> JSONResponse:
try:
form = list((await request.form).multi_items())
except HTTPException:
form = {}
try:
json = await request.json
except HTTPException:
json = {}
try:
if not (form or json):
content = (await request.body).decode()
else:
content = ""
except UnicodeDecodeError:
content = ""
return JSONResponse(
{
"method": request.method,
"urlpath": request.url.path,
"headers": dict(request.headers),
"form": form,
"json": json,
"content": content,
}
)