📝 Add docs for custom response

This commit is contained in:
Sebastián Ramírez
2018-12-15 15:20:22 +04:00
parent 0df2720490
commit f6667ecfb0
7 changed files with 185 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
from fastapi import FastAPI
from starlette.responses import UJSONResponse
app = FastAPI()
@app.get("/items/", content_type=UJSONResponse)
async def read_items():
return [{"item_id": "Foo"}]

View File

@@ -0,0 +1,18 @@
from fastapi import FastAPI
from starlette.responses import HTMLResponse
app = FastAPI()
@app.get("/items/", content_type=HTMLResponse)
async def read_items():
return """
<html>
<head>
<title>Some HTML in here</title>
</head>
<body>
<h1>Look ma! HTML!</h1>
</body>
</html>
"""

View File

@@ -0,0 +1,19 @@
from fastapi import FastAPI
from starlette.responses import HTMLResponse
app = FastAPI()
@app.get("/items/")
async def read_items():
html_content = """
<html>
<head>
<title>Some HTML in here</title>
</head>
<body>
<h1>Look ma! HTML!</h1>
</body>
</html>
"""
return HTMLResponse(content=html_content, status_code=200)

View File

@@ -0,0 +1,23 @@
from fastapi import FastAPI
from starlette.responses import HTMLResponse
app = FastAPI()
def generate_html_response():
html_content = """
<html>
<head>
<title>Some HTML in here</title>
</head>
<body>
<h1>Look ma! HTML!</h1>
</body>
</html>
"""
return HTMLResponse(content=html_content, status_code=200)
@app.get("/items/", content_type=HTMLResponse)
async def read_items():
return generate_html_response()