V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
abersheeran
V2EX  ›  分享创造

迫于测试网关写了个 httpbin

  •  
  •   abersheeran ·
    abersheeran · 2021-12-31 19:12:03 +08:00 · 1165 次点击
    这是一个创建于 1001 天前的主题,其中的信息可能已经有所发展或是发生改变。

    要是 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,
            }
        )
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1159 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 23:27 · PVG 07:27 · LAX 16:27 · JFK 19:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.