コード例 #1
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     body = b""
     async for chunk in request.stream():
         body += chunk
     response = JSONResponse({"body": body.decode()})
     await response(scope, receive, send)
コード例 #2
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     response = JSONResponse({
         "host": request.client.host,
         "port": request.client.port
     })
     await response(scope, receive, send)
コード例 #3
0
 async def app(scope, receive, send):
     request = Request(scope)
     try:
         data = await request.json
     except NotImplementedError:
         data = "Receive channel not available"
     response = JSONResponse({"json": data})
     await response(scope, receive, send)
コード例 #4
0
    async def app(scope, receive, send):
        nonlocal disconnected_after_response

        request = Request(scope, receive)
        await request.body
        disconnected = await request.is_disconnected()
        response = JSONResponse({"disconnected": disconnected})
        await response(scope, receive, send)
        disconnected_after_response = await request.is_disconnected()
コード例 #5
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     form = await request.form
     file = form["file-key"]
     assert isinstance(file, UploadFile)
     assert await file.aread() == b"temporary file"
     response = JSONResponse({"file": file.filename})
     await response(scope, receive, send)
     await request.close()
コード例 #6
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     chunks = b""
     async for chunk in request.stream():
         chunks += chunk
     try:
         body = await request.body
     except RuntimeError:
         body = b"<stream consumed>"
     response = JSONResponse({"body": body.decode(), "stream": chunks.decode()})
     await response(scope, receive, send)
コード例 #7
0
async def api(request: Request) -> Response:
    """Check headers for authorization, load JSON/query data and return as JSON."""
    if request.method != "PUT":
        return Response(405)

    if request.headers.get("authorization") is None:
        return PlainTextResponse("ERROR", status_code=401)

    return JSONResponse({
        "params": request.path_params,
        "query": dict(request.query_params),
        "data": await request.json,
    })
コード例 #8
0
ファイル: responses.py プロジェクト: yianz/index.py
def _json(body,
          status: int = 200,
          headers: typing.Mapping[str, str] = None) -> HttpResponse:
    return JSONResponse(body, status, headers)
コード例 #9
0
 async def path(request: Request) -> Response:
     return JSONResponse(request.path_params)
コード例 #10
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     params = dict(request.query_params)
     response = JSONResponse({"params": params})
     await response(scope, receive, send)
コード例 #11
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     data = {"method": request.method, "url": str(request.url)}
     response = JSONResponse(data)
     await response(scope, receive, send)
コード例 #12
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     response = JSONResponse({"cookies": request.cookies})
     await response(scope, receive, send)
コード例 #13
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     data = await request.json
     response = JSONResponse({"json": data})
     await response(scope, receive, send)
コード例 #14
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     form = await request.form
     response = JSONResponse({"form": dict(form)})
     await response(scope, receive, send)
     await request.close()
コード例 #15
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     body = await request.body
     response = JSONResponse({"body": body.decode()})
     await response(scope, receive, send)
コード例 #16
0
 async def app(scope, receive, send):
     request = Request(scope, receive)
     headers = dict(request.headers)
     headers.pop("user-agent")  # this is httpx version, delete it
     response = JSONResponse({"headers": headers})
     await response(scope, receive, send)