迫于测试网关写了个 httpbin

2021-12-31 19:12:03 +08:00
 abersheeran

要是 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,
        }
    )
1165 次点击
所在节点    分享创造
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/825573

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX