爱意满满的作品展示区。
abersheeran

迫于测试网关写了个 httpbin

  •  
  •   abersheeran · Dec 31, 2021 · 1521 views
    This topic created in 1601 days ago, the information mentioned may be changed or developed.

    要是 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,
            }
        )
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2431 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 16:04 · PVG 00:04 · LAX 09:04 · JFK 12:04
    ♥ Do have faith in what you're doing.