Phản hồi trong HTML là gì?

Loại phản hồi này được thể hiện với sự trợ giúp của lớp

use Opis\Http\Responses\StringResponse;

$response = new StringResponse('Hello');

echo $response->getHeader('Content-Length'); //> 5
3 và nó cho phép bạn gửi một chuỗi dưới dạng phản hồi HTTP. Hàm tạo của lớp này có chữ ký sau

Show
public function __construct(
    string $body, 
    int $status = 200, 
    array $headers = []
);
  • use Opis\Http\Responses\StringResponse;
    
    $response = new StringResponse('Hello');
    
    echo $response->getHeader('Content-Length'); //> 5
    
    4 - nội dung thư
  • use Opis\Http\Responses\StringResponse;
    
    $response = new StringResponse('Hello');
    
    echo $response->getHeader('Content-Length'); //> 5
    
    5 - mã trạng thái phản hồi
  • use Opis\Http\Responses\StringResponse;
    
    $response = new StringResponse('Hello');
    
    echo $response->getHeader('Content-Length'); //> 5
    
    6 - một danh sách các tiêu đề

Theo mặc định, lớp này tự động thêm tiêu đề

use Opis\Http\Responses\StringResponse;

$response = new StringResponse('Hello');

echo $response->getHeader('Content-Length'); //> 5
7,
public function __construct(
    $body, 
    int $status = 200, 
    array $headers = []
);
5 và
public function __construct(
    $json,
    int $status = 200,
    array $headers = [],
    int $encodeOptions = JSON_UNESCAPED_SLASHES
);
9 vào phản hồi

Nhưng nếu bạn trực tiếp trả về một _______8_______9, dữ liệu sẽ không được chuyển đổi tự động và tài liệu sẽ không được tạo tự động (ví dụ: bao gồm "loại phương tiện" cụ thể trong tiêu đề HTTP ___9_______1 như một phần của OpenAPI được tạo)

Nhưng bạn cũng có thể khai báo

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 mà bạn muốn sử dụng, trong trình trang trí thao tác đường dẫn

Nội dung mà bạn trả về từ hàm thao tác đường dẫn của bạn sẽ được đặt bên trong

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 đó

Và nếu

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 đó có loại phương tiện JSON (
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
5), giống như trường hợp của
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
8 và
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
7, thì dữ liệu bạn trả về sẽ được tự động chuyển đổi (và được lọc) với bất kỳ
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
8 Pydantic nào mà bạn đã khai báo trong trình trang trí thao tác đường dẫn

Ghi chú

Nếu bạn sử dụng lớp phản hồi không có loại phương tiện, FastAPI sẽ mong đợi phản hồi của bạn không có nội dung, do đó, nó sẽ không ghi lại định dạng phản hồi trong tài liệu OpenAPI được tạo

Sử dụng from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/") async def read_items(): html_content = """ Some HTML in here

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) 9

Ví dụ: nếu bạn đang siết chặt hiệu suất, bạn có thể cài đặt và sử dụng

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
0 và đặt phản hồi là
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
9

Nhập lớp

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 (lớp con) bạn muốn sử dụng và khai báo nó trong trình trang trí thao tác đường dẫn

Đối với các phản hồi lớn, trả lại trực tiếp một

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 nhanh hơn nhiều so với trả về một từ điển

Điều này là do theo mặc định, FastAPI sẽ kiểm tra mọi mục bên trong và đảm bảo rằng nó có thể tuần tự hóa với JSON, sử dụng cùng Bộ mã hóa tương thích JSON được giải thích trong hướng dẫn. Đây là thứ cho phép bạn trả về các đối tượng tùy ý, ví dụ như các mô hình cơ sở dữ liệu

Nhưng nếu bạn chắc chắn rằng nội dung mà bạn đang trả về có thể tuần tự hóa với JSON, thì bạn có thể chuyển trực tiếp nội dung đó tới lớp phản hồi và tránh chi phí phát sinh thêm mà FastAPI sẽ phải chịu bằng cách chuyển nội dung trả về của bạn qua

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
4 trước khi chuyển nội dung đó tới lớp phản hồi

from fastapi import FastAPI
from fastapi.responses import ORJSONResponse

app = FastAPI()


@app.get("/items/", response_class=ORJSONResponse)
async def read_items():
    return ORJSONResponse([{"item_id": "Foo"}])

Thông tin

Tham số

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5 cũng sẽ được sử dụng để xác định "loại phương tiện" của phản hồi

Trong trường hợp này, tiêu đề HTTP

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
1 sẽ được đặt thành
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
5

Và nó sẽ được ghi lại như vậy trong OpenAPI

Mẹo

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
9 hiện chỉ khả dụng trong FastAPI, không có trong Starlette

Phản hồi HTML

Để trả lời phản hồi bằng HTML trực tiếp từ FastAPI, hãy sử dụng

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
9

  • Nhập khẩu
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    def generate_html_response():
        html_content = """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """ return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
    9
  • Vượt qua
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    def generate_html_response():
        html_content = """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """ return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
    9 làm tham số
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    def generate_html_response():
        html_content = """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """ return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
    5 của trình trang trí thao tác đường dẫn của bạn

________số 8_______

Thông tin

Tham số

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5 cũng sẽ được sử dụng để xác định "loại phương tiện" của phản hồi

Trong trường hợp này, tiêu đề HTTP

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
1 sẽ được đặt thành
from fastapi import FastAPI, Response

app = FastAPI()


@app.get("/legacy/")
def get_legacy_data():
    data = """
    
    
Apply shampoo here.
You'll have to use soap here. """ return Response(content=data, media_type="application/xml")
5

Và nó sẽ được ghi lại như vậy trong OpenAPI

Trả lại một from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/", response_class=HTMLResponse) async def read_items(): return """ Some HTML in here

Look ma! HTML!

""" 9

Như đã thấy trong Trả lại phản hồi trực tiếp, bạn cũng có thể ghi đè trực tiếp phản hồi trong thao tác đường dẫn của mình bằng cách trả lại phản hồi đó

Ví dụ tương tự ở trên, trả về một

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
9, có thể giống như

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)

Cảnh báo

Một

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 được trả về trực tiếp bởi chức năng vận hành đường dẫn của bạn sẽ không được ghi lại trong OpenAPI (ví dụ:
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
1 sẽ không được ghi lại) và sẽ không hiển thị trong tài liệu tương tác tự động

Thông tin

Tất nhiên, tiêu đề, mã trạng thái, v.v. thực tế của

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
1, sẽ đến từ đối tượng
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 mà bạn đã trả về

Tài liệu trong OpenAPI và ghi đè from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/", response_class=HTMLResponse) async def read_items(): return """ Some HTML in here

Look ma! HTML!

""" 9

Nếu bạn muốn ghi đè phản hồi từ bên trong hàm nhưng đồng thời ghi lại "loại phương tiện" trong OpenAPI, bạn có thể sử dụng tham số

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5 VÀ trả về đối tượng
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9

Sau đó,

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5 sẽ chỉ được sử dụng để ghi lại hoạt động của đường dẫn OpenAPI, nhưng
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 của bạn sẽ được sử dụng như hiện tại

Trả lại một
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
9 trực tiếp

Ví dụ, nó có thể là một cái gì đó giống như

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()

Trong ví dụ này, hàm

from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

app = FastAPI()


@app.get("/", response_class=PlainTextResponse)
async def main():
    return "Hello World"
8 đã tạo và trả về một
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 thay vì trả về HTML trong một
from fastapi import FastAPI
from fastapi.responses import UJSONResponse

app = FastAPI()


@app.get("/items/", response_class=UJSONResponse)
async def read_items():
    return [{"item_id": "Foo"}]
0

Bằng cách trả lại kết quả của việc gọi

from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

app = FastAPI()


@app.get("/", response_class=PlainTextResponse)
async def main():
    return "Hello World"
8, bạn đã trả lại một
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 sẽ ghi đè lên hành vi FastAPI mặc định

Nhưng khi bạn vượt qua

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
9 trong
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5, FastAPI sẽ biết cách ghi lại nó trong OpenAPI và các tài liệu tương tác dưới dạng HTML với
from fastapi import FastAPI, Response

app = FastAPI()


@app.get("/legacy/")
def get_legacy_data():
    data = """
    
    
Apply shampoo here.
You'll have to use soap here. """ return Response(content=data, media_type="application/xml")
5

Phản hồi trong HTML là gì?

phản hồi có sẵn

Dưới đây là một số câu trả lời có sẵn

Hãy nhớ rằng bạn có thể sử dụng

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 để trả lại bất kỳ thứ gì khác hoặc thậm chí tạo một lớp con tùy chỉnh

chi tiết kỹ thuật

Bạn cũng có thể sử dụng

from fastapi import FastAPI
from fastapi.responses import UJSONResponse

app = FastAPI()


@app.get("/items/", response_class=UJSONResponse)
async def read_items():
    return [{"item_id": "Foo"}]
7

FastAPI cung cấp

from fastapi import FastAPI
from fastapi.responses import UJSONResponse

app = FastAPI()


@app.get("/items/", response_class=UJSONResponse)
async def read_items():
    return [{"item_id": "Foo"}]
8 giống như
from fastapi import FastAPI
from fastapi.responses import UJSONResponse

app = FastAPI()


@app.get("/items/", response_class=UJSONResponse)
async def read_items():
    return [{"item_id": "Foo"}]
9 chỉ để thuận tiện cho bạn, nhà phát triển. Nhưng hầu hết các phản hồi có sẵn đến trực tiếp từ Starlette

from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/", response_class=HTMLResponse) async def read_items(): return """ Some HTML in here

Look ma! HTML!

""" 9

Lớp

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 chính, tất cả các phản hồi khác kế thừa từ nó

Bạn có thể trả lại trực tiếp

Nó chấp nhận các tham số sau

  • from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    2 - Một
    from fastapi import FastAPI
    from fastapi.responses import UJSONResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=UJSONResponse)
    async def read_items():
        return [{"item_id": "Foo"}]
    
    0 hoặc
    from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    4
  • from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    5 - Mã trạng thái HTTP
    from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    6
  • from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    7 - Một
    from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    8 chuỗi
  • from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    9 - Một
    from fastapi import FastAPI
    from fastapi.responses import UJSONResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=UJSONResponse)
    async def read_items():
        return [{"item_id": "Foo"}]
    
    0 đưa ra loại phương tiện. e. g.
    from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/fastapi", response_class=RedirectResponse)
    async def redirect_fastapi():
        return "https://fastapi.tiangolo.com"
    
    1

FastAPI (thực ra là Starlette) sẽ tự động bao gồm tiêu đề Độ dài nội dung. Nó cũng sẽ bao gồm tiêu đề Loại nội dung, dựa trên media_type và nối thêm bộ ký tự cho các loại văn bản

from fastapi import FastAPI, Response

app = FastAPI()


@app.get("/legacy/")
def get_legacy_data():
    data = """
    
    
Apply shampoo here.
You'll have to use soap here. """ return Response(content=data, media_type="application/xml")

from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() def generate_html_response(): html_content = """ Some HTML in here

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response() 9

Nhận một số văn bản hoặc byte và trả về phản hồi HTML, như bạn đã đọc ở trên

from fastapi import FastAPI from fastapi.responses import RedirectResponse app = FastAPI() @app.get("/fastapi", response_class=RedirectResponse) async def redirect_fastapi(): return "https://fastapi.tiangolo.com" 3

Lấy một số văn bản hoặc byte và trả về một phản hồi văn bản thuần túy

from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

app = FastAPI()


@app.get("/", response_class=PlainTextResponse)
async def main():
    return "Hello World"

from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/", response_class=HTMLResponse) async def read_items(): return """ Some HTML in here

Look ma! HTML!

""" 8

Lấy một số dữ liệu và trả về phản hồi được mã hóa

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
5

Đây là phản hồi mặc định được sử dụng trong FastAPI, như bạn đã đọc ở trên

from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/") async def read_items(): html_content = """ Some HTML in here

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) 9

Phản hồi JSON thay thế nhanh bằng cách sử dụng

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
0, như bạn đã đọc ở trên

from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/") async def read_items(): html_content = """ Some HTML in here

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) 7

Một phản hồi JSON thay thế bằng cách sử dụng

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/fastapi", response_class=RedirectResponse)
async def redirect_fastapi():
    return "https://fastapi.tiangolo.com"
9

Cảnh báo

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/fastapi", response_class=RedirectResponse)
async def redirect_fastapi():
    return "https://fastapi.tiangolo.com"
9 kém cẩn thận hơn so với triển khai tích hợp sẵn của Python trong cách xử lý một số trường hợp cạnh

from fastapi import FastAPI
from fastapi.responses import UJSONResponse

app = FastAPI()


@app.get("/items/", response_class=UJSONResponse)
async def read_items():
    return [{"item_id": "Foo"}]

Mẹo

Có thể là

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
9 có thể là một sự thay thế nhanh hơn

from fastapi import FastAPI from fastapi.responses import RedirectResponse app = FastAPI() @app.get("/pydantic", response_class=RedirectResponse, status_code=302) async def redirect_pydantic(): return "https://pydantic-docs.helpmanual.io/" 2

Trả về một chuyển hướng HTTP. Sử dụng mã trạng thái 307 (Chuyển hướng tạm thời) theo mặc định

Bạn có thể trả lại một

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/pydantic", response_class=RedirectResponse, status_code=302)
async def redirect_pydantic():
    return "https://pydantic-docs.helpmanual.io/"
2 trực tiếp

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/typer")
async def redirect_typer():
    return RedirectResponse("https://typer.tiangolo.com")


Hoặc bạn có thể sử dụng nó trong tham số

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/fastapi", response_class=RedirectResponse)
async def redirect_fastapi():
    return "https://fastapi.tiangolo.com"

Nếu bạn làm điều đó, thì bạn có thể trả lại URL trực tiếp từ chức năng thao tác đường dẫn của mình

Trong trường hợp này,

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/typer")
async def redirect_typer():
    return RedirectResponse("https://typer.tiangolo.com")
5 được sử dụng sẽ là giá trị mặc định cho
from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/pydantic", response_class=RedirectResponse, status_code=302)
async def redirect_pydantic():
    return "https://pydantic-docs.helpmanual.io/"
2, là
from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/pydantic", response_class=RedirectResponse, status_code=302)
async def redirect_pydantic():
    return "https://pydantic-docs.helpmanual.io/"
7


Bạn cũng có thể sử dụng tham số

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/typer")
async def redirect_typer():
    return RedirectResponse("https://typer.tiangolo.com")
5 kết hợp với tham số
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/pydantic", response_class=RedirectResponse, status_code=302)
async def redirect_pydantic():
    return "https://pydantic-docs.helpmanual.io/"

from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/", response_class=HTMLResponse) async def read_items(): return """ Some HTML in here

Look ma! HTML!

""" 00

Lấy một trình tạo không đồng bộ hoặc một trình tạo/trình lặp thông thường và truyền nội dung phản hồi

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
0

Sử dụng
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
00 với các đối tượng giống như tệp

Nếu bạn có một đối tượng giống như tệp (e. g. đối tượng được trả về bởi

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
02), bạn có thể tạo một hàm tạo để lặp lại đối tượng giống như tệp đó

Bằng cách đó, bạn không cần phải đọc tất cả trước tiên trong bộ nhớ và bạn có thể chuyển chức năng tạo đó cho

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
00 và trả lại

Điều này bao gồm nhiều thư viện để tương tác với lưu trữ đám mây, xử lý video và các thư viện khác

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
1

  1. Đây là hàm tạo. Đó là một "hàm tạo" bởi vì nó chứa các câu lệnh
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """
    04 bên trong
  2. Bằng cách sử dụng khối
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """
    05, chúng tôi đảm bảo rằng đối tượng giống như tệp được đóng sau khi hoàn thành chức năng tạo. Vì vậy, sau khi gửi xong phản hồi
  3. Điều này

    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """
    06 yêu cầu hàm lặp lại thứ đó có tên là
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """
    07. Và sau đó, đối với mỗi phần được lặp lại, mang lại phần đó như đến từ hàm tạo này

    Vì vậy, nó là một hàm tạo chuyển công việc "tạo" sang một thứ khác trong nội bộ

    Bằng cách làm theo cách này, chúng ta có thể đặt nó vào một khối

    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """
    05 và theo cách đó, đảm bảo rằng nó sẽ được đóng lại sau khi hoàn thành

Mẹo

Lưu ý rằng ở đây vì chúng tôi đang sử dụng tiêu chuẩn

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
02 không hỗ trợ
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
10 và
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
11, chúng tôi khai báo thao tác đường dẫn với
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
12 bình thường

from fastapi import FastAPI from fastapi.responses import HTMLResponse app = FastAPI() @app.get("/items/", response_class=HTMLResponse) async def read_items(): return """ Some HTML in here

Look ma! HTML!

""" 13

Truyền không đồng bộ một tệp dưới dạng phản hồi

Lấy một nhóm đối số khác để khởi tạo so với các loại phản hồi khác

  • from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """
    14 - Đường dẫn tệp đến tệp để phát trực tuyến
  • from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    7 - Bất kỳ tiêu đề tùy chỉnh nào để đưa vào, dưới dạng từ điển
  • from fastapi import FastAPI
    from fastapi.responses import RedirectResponse
    
    app = FastAPI()
    
    
    @app.get("/typer")
    async def redirect_typer():
        return RedirectResponse("https://typer.tiangolo.com")
    
    9 - Một chuỗi cho biết loại phương tiện. Nếu không được đặt, tên tệp hoặc đường dẫn sẽ được dùng để suy ra loại phương tiện
  • from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """
    17 - Nếu được đặt, điều này sẽ được bao gồm trong phản hồi
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        
            
                Some HTML in here
            
            
                

    Look ma! HTML!

    """
    18

Phản hồi tệp sẽ bao gồm các tiêu đề thích hợp

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
19,
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
20 và
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
21

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
2

Bạn cũng có thể sử dụng tham số

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
3

Trong trường hợp này, bạn có thể trả lại đường dẫn tệp trực tiếp từ chức năng thao tác đường dẫn của mình

Lớp phản hồi tùy chỉnh

Bạn có thể tạo lớp phản hồi tùy chỉnh của riêng mình, kế thừa từ

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
9 và sử dụng nó

Ví dụ: giả sử bạn muốn sử dụng

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
0, nhưng với một số cài đặt tùy chỉnh không được sử dụng trong lớp
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
9 được bao gồm

Giả sử bạn muốn nó trả về JSON được định dạng và thụt lề, vì vậy bạn muốn sử dụng tùy chọn orjson

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
26

Bạn có thể tạo một

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
27. Điều chính bạn phải làm là tạo một phương thức
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
28 trả về nội dung là
from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/typer")
async def redirect_typer():
    return RedirectResponse("https://typer.tiangolo.com")
4

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
4

Bây giờ thay vì trở lại

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
5

phản hồi này sẽ trở lại

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
6

Tất nhiên, bạn có thể sẽ tìm thấy nhiều cách tốt hơn để tận dụng điều này hơn là định dạng JSON. 😉

Lớp phản hồi mặc định

Khi tạo một thể hiện của lớp FastAPI hoặc một

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
30, bạn có thể chỉ định lớp phản hồi nào sẽ được sử dụng theo mặc định

Tham số xác định điều này là

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
31

Trong ví dụ bên dưới, FastAPI sẽ sử dụng

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200)
9 theo mặc định, trong tất cả các thao tác đường dẫn, thay vì
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
8

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
7

Mẹo

Bạn vẫn có thể ghi đè

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

""" return HTMLResponse(content=html_content, status_code=200) @app.get("/items/", response_class=HTMLResponse) async def read_items(): return generate_html_response()
5 trong thao tác đường dẫn như trước đây

tài liệu bổ sung

Bạn cũng có thể khai báo loại phương tiện và nhiều chi tiết khác trong OpenAPI bằng cách sử dụng

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    
        
            Some HTML in here
        
        
            

Look ma! HTML!

"""
35. Phản hồi bổ sung trong OpenAPI

Dữ liệu phản hồi là gì?

Dữ liệu phản hồi có nghĩa là câu trả lời của chủ thể dữ liệu đối với các câu hỏi do Công ty đặt ra khi sử dụng Dịch vụ . Dữ liệu phản hồi được lưu trữ tách biệt với dữ liệu cá nhân còn lại nhưng được liên kết với chủ thể dữ liệu thông qua tài khoản của người đó với bộ xử lý dữ liệu cá nhân.

Phản hồi trong JavaScript là gì?

Trong JavaScript, dữ liệu phản hồi có sẵn trong các thuộc tính phản hồi và responseText của đối tượng XMLHttpRequest sau khi yêu cầu hoàn tất . Trong jQuery, dữ liệu phản hồi được truyền dưới dạng tham số đầu tiên cho hàm xử lý AJAX mà bạn xác định. Kịch bản và văn bản được xử lý khá đơn giản bởi.

Phản hồi trong API là gì?

Phản hồi xác định mã trạng thái HTTP và dữ liệu được trả về trong nội dung phản hồi và tiêu đề . Bạn có thể tạo phản hồi cho các thao tác Đường dẫn trong định nghĩa API của mình. Chỉnh sửa phản hồi. Phản hồi xác định mã trạng thái HTTP và dữ liệu được trả về trong nội dung phản hồi và tiêu đề.

cơ quan phản ứng là gì?

Nội dung phản hồi chứa thông tin được yêu cầu ở định dạng được chỉ định bởi trường Chấp nhận trong tiêu đề yêu cầu . Đối với JSON, phần thân thường là một đối tượng hoặc một mảng đối tượng. Các phần sau đây cung cấp các quy tắc định dạng cơ bản cho JSON. Các quy tắc này cũng áp dụng cho nội dung yêu cầu.