示例#1
0
 def app(environ, start_response):
     request = Request(environ)
     if request.accepts("application/json"):
         response = JSONResponse({"data": data})
     else:
         response = PlainTextResponse(data)
     return response(environ, start_response)
示例#2
0
 def app(environ, start_response):
     request = Request(environ)
     response = JSONResponse({
         "host": request.client.host,
         "port": request.client.port
     })
     return response(environ, start_response)
示例#3
0
def app_read_body(environ, start_response):
    request = Request(environ)
    data = request.form
    output = {}
    for key, value in data.items():
        output[key] = value
    request.close()
    response = JSONResponse(output)
    return response(environ, start_response)
示例#4
0
 def app(environ, start_response):
     request = Request(environ)
     form = request.form
     file = form["file-key"]
     assert isinstance(file, UploadFile)
     assert file.read() == b"temporary file"
     response = JSONResponse({"file": file.filename})
     request.close()
     return response(environ, start_response)
示例#5
0
 def app(environ, start_response):
     request = Request(environ)
     body = request.body
     chunks = b""
     for chunk in request.stream():
         chunks += chunk
     response = JSONResponse({
         "body": body.decode(),
         "stream": chunks.decode()
     })
     return response(environ, start_response)
示例#6
0
 def app(environ, start_response):
     request = Request(environ)
     chunks = b""
     for chunk in request.stream():
         chunks += chunk
     try:
         body = request.body
     except RuntimeError:
         body = b"<stream consumed>"
     response = JSONResponse({
         "body": body.decode(),
         "stream": chunks.decode()
     })
     return response(environ, start_response)
示例#7
0
def app(environ, start_response):
    request = Request(environ)
    data = request.form
    output = {}
    for key, value in data.items():
        if isinstance(value, UploadFile):
            content = value.read()
            output[key] = {
                "filename": value.filename,
                "content": content.decode(),
                "content_type": value.content_type,
            }
        else:
            output[key] = value
    request.close()
    response = JSONResponse(output)
    return response(environ, start_response)
示例#8
0
def multi_items_app(environ, start_response):
    request = Request(environ)
    data = request.form
    output = {}
    for key, value in data.multi_items():
        if key not in output:
            output[key] = []
        if isinstance(value, UploadFile):
            content = value.read()
            output[key].append({
                "filename": value.filename,
                "content": content.decode(),
                "content_type": value.content_type,
            })
        else:
            output[key].append(value)
    request.close()
    response = JSONResponse(output)
    return response(environ, start_response)
示例#9
0
 def app(environ, start_response):
     request = Request(environ)
     headers = dict(request.headers)
     headers.pop("user-agent")  # this is httpx version, delete it
     response = JSONResponse({"headers": headers})
     return response(environ, start_response)
示例#10
0
 def app(environ, start_response):
     request = Request(environ)
     params = dict(request.query_params)
     response = JSONResponse({"params": params})
     return response(environ, start_response)
示例#11
0
 def path(request: Request) -> Response:
     return JSONResponse(request.path_params)
示例#12
0
 def app(environ, start_response):
     request = Request(environ)
     data = request.json
     response = JSONResponse({"json": data})
     return response(environ, start_response)
示例#13
0
 def app(environ, start_response):
     request = Request(environ)
     form = request.form
     response = JSONResponse({"form": dict(form)})
     return response(environ, start_response)
     request.close()
示例#14
0
 def app(environ, start_response):
     request = Request(environ)
     body = request.body
     response = JSONResponse({"body": body.decode()})
     return response(environ, start_response)