def app(environ, start_response): request = Request(environ) body = b"" for chunk in request.stream(): body += chunk response = PlainTextResponse(body) return response(environ, start_response)
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) 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_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) 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) response = JSONResponse({ "host": request.client.host, "port": request.client.port }) 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 test_request_environ_interface(): """ A Request can be instantiated with a environ, and presents a `Mapping` interface. """ request = Request({"type": "http", "method": "GET", "path": "/abc/"}) assert request["method"] == "GET" assert dict(request) == {"type": "http", "method": "GET", "path": "/abc/"} assert len(request) == 3 # test eq assert request == Request({ "type": "http", "method": "GET", "path": "/abc/" }) assert request != Request({ "type": "http", "method": "GET", "path": "/abc/", "query_params": {} }) assert request != dict({"type": "http", "method": "GET", "path": "/abc/"})
def test_request_client(): def app(environ, start_response): request = Request(environ) response = JSONResponse({ "host": request.client.host, "port": request.client.port }) return response(environ, start_response) with httpx.Client(app=app, base_url="http://testServer/") as client: response = client.get("/") assert response.json() == {"host": None, "port": None} request = Request({"REMOTE_ADDR": "127.0.0.1", "REMOTE_PORT": "62124"}) assert request.client == Address("127.0.0.1", 62124)
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) params = dict(request.query_params) response = JSONResponse({"params": params}) 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) response = PlainTextResponse(request.method + " " + str(request.url)) return response(environ, start_response)
def path(request: Request) -> Response: return PlainTextResponse(request.get("PATH_INFO", ""))
def root(request: Request) -> Response: return PlainTextResponse(request.get("SCRIPT_NAME", ""))
def app(environ, start_response): request = Request(environ) body = request.body response = JSONResponse({"body": body.decode()}) return response(environ, start_response)
def app(environ, start_response): request = Request(environ) data = request.json response = JSONResponse({"json": data}) return response(environ, start_response)