[FastAPI-20]设置响应头

from fastapi import FastAPI, Response
from fastapi.responses import JSONResponse
from pydantic import BaseModel

app = FastAPI()


class User(BaseModel):
    username: str
    password: str


'''
响应头 Response
'''


# @app.post("/login")
# def login(response: Response):
#     response.headers["x-jwt-token"] = "This is jwt-token"
#     response.headers.append("x-token","This is token")


@app.post("/login")
def login(user: User):
    response = JSONResponse(
        {
            "username": user.username,
            "password": user.password
        },
        status_code=201,
        headers={"x-jwt-token": "value"}
    )
    return response
posted @ 2023-03-25 11:12  LeoShi2020  阅读(129)  评论(0编辑  收藏  举报