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)
def app(environ, start_response): request = Request(environ) response = JSONResponse({ "host": request.client.host, "port": request.client.port }) return response(environ, start_response)
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)
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)
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)
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)
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)
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)
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)
def app(environ, start_response): request = Request(environ) params = dict(request.query_params) response = JSONResponse({"params": params}) return response(environ, start_response)
def path(request: Request) -> Response: return JSONResponse(request.path_params)
def app(environ, start_response): request = Request(environ) data = request.json response = JSONResponse({"json": data}) return response(environ, start_response)
def app(environ, start_response): request = Request(environ) form = request.form response = JSONResponse({"form": dict(form)}) return response(environ, start_response) request.close()
def app(environ, start_response): request = Request(environ) body = request.body response = JSONResponse({"body": body.decode()}) return response(environ, start_response)