Search Options

Results per page
Sort
Preferred Languages
Advance

Results 1 - 10 of 77 for JSONResponse (0.16 sec)

  1. docs/zh/docs/advanced/additional-status-codes.md

    # 额外的状态码
    
    **FastAPI** 默认使用 `JSONResponse` 返回一个响应,将你的 *路径操作* 中的返回内容放到该 `JSONResponse` 中。
    
    **FastAPI** 会自动使用默认的状态码或者使用你在 *路径操作* 中设置的状态码。
    
    ## 额外的状态码
    
    如果你想要返回主要状态码之外的状态码,你可以通过直接返回一个 `Response` 来实现,比如 `JSONResponse`,然后直接设置额外的状态码。
    
    例如,假设你想有一个 *路径操作* 能够更新条目,并且更新成功时返回 200 「成功」 的 HTTP 状态码。
    
    但是你也希望它能够接受新的条目。并且当这些条目不存在时,会自动创建并返回 201 「创建」的 HTTP 状态码。
    
    要实现它,导入 `JSONResponse`,然后在其中直接返回你的内容,并将 `status_code` 设置为为你要的值。
    
    ```Python hl_lines="4  25"
    Plain Text
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Sat May 14 11:59:59 GMT 2022
    - 1.8K bytes
    - Viewed (0)
  2. docs/zh/docs/advanced/response-directly.md

    然后,**FastAPI** 会在后台将这些兼容 JSON 的数据(比如字典)放到一个 `JSONResponse` 中,该 `JSONResponse` 会用来发送响应给客户端。
    
    但是你可以在你的 *路径操作* 中直接返回一个 `JSONResponse`。
    
    直接返回响应可能会有用处,比如返回自定义的响应头和 cookies。
    
    ## 返回 `Response`
    
    事实上,你可以返回任意 `Response` 或者任意 `Response` 的子类。
    
    !!! tip "小贴士"
        `JSONResponse` 本身是一个 `Response` 的子类。
    
    当你返回一个 `Response` 时,**FastAPI** 会直接传递它。
    
    Plain Text
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Thu May 12 00:06:16 GMT 2022
    - 3.1K bytes
    - Viewed (0)
  3. docs/es/docs/advanced/response-directly.md

    Luego, tras bastidores, pondría esos datos compatibles con JSON (por ejemplo, un `dict`) dentro de una `JSONResponse` que se usaría para enviar la respuesta al cliente.
    
    Pero puedes devolver una `JSONResponse` directamente de tu *operación de path*.
    
    Esto puede ser útil, por ejemplo, para devolver cookies o headers personalizados.
    
    ## Devolver una `Response`
    
    Plain Text
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Sat Jan 13 11:57:27 GMT 2024
    - 3.2K bytes
    - Viewed (0)
  4. docs/ja/docs/advanced/response-directly.md

    このとき背後では、JSON互換なデータ (例えば`dict`) を、クライアントへ送信されるレスポンスとして利用される `JSONResponse` の中に含めます。
    
    しかし、*path operation* から `JSONResponse` を直接返すこともできます。
    
    これは例えば、カスタムヘッダーやcookieを返すときに便利です。
    
    ## `Response` を返す
    
    実際は、`Response` やそのサブクラスを返すことができます。
    
    !!! tip "豆知識"
        `JSONResponse` それ自体は、 `Response` のサブクラスです。
    
    `Response` を返した場合は、**FastAPI** は直接それを返します。
    
    Plain Text
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Thu Nov 05 22:50:38 GMT 2020
    - 3.6K bytes
    - Viewed (0)
  5. docs_src/handling_errors/tutorial005.py

    from fastapi.exceptions import RequestValidationError
    from fastapi.responses import JSONResponse
    from pydantic import BaseModel
    
    app = FastAPI()
    
    
    @app.exception_handler(RequestValidationError)
    async def validation_exception_handler(request: Request, exc: RequestValidationError):
        return JSONResponse(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
    Python
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Thu Mar 26 19:09:53 GMT 2020
    - 667 bytes
    - Viewed (0)
  6. docs_src/handling_errors/tutorial003.py

    from fastapi import FastAPI, Request
    from fastapi.responses import JSONResponse
    
    
    class UnicornException(Exception):
        def __init__(self, name: str):
            self.name = name
    
    
    app = FastAPI()
    
    
    @app.exception_handler(UnicornException)
    async def unicorn_exception_handler(request: Request, exc: UnicornException):
        return JSONResponse(
            status_code=418,
    Python
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Thu Mar 26 19:09:53 GMT 2020
    - 626 bytes
    - Viewed (0)
  7. docs_src/additional_responses/tutorial003.py

    from fastapi import FastAPI
    from fastapi.responses import JSONResponse
    from pydantic import BaseModel
    
    
    class Item(BaseModel):
        id: str
        value: str
    
    
    class Message(BaseModel):
        message: str
    
    
    app = FastAPI()
    
    
    @app.get(
        "/items/{item_id}",
        response_model=Item,
        responses={
            404: {"model": Message, "description": "The item was not found"},
            200: {
    Python
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Thu Mar 26 19:09:53 GMT 2020
    - 837 bytes
    - Viewed (0)
  8. tests/test_additional_responses_response_class.py

    import typing
    
    from fastapi import FastAPI
    from fastapi.responses import JSONResponse
    from fastapi.testclient import TestClient
    from pydantic import BaseModel
    
    app = FastAPI()
    
    
    class JsonApiResponse(JSONResponse):
        media_type = "application/vnd.api+json"
    
    
    class Error(BaseModel):
        status: str
        title: str
    
    
    class JsonApiError(BaseModel):
        errors: typing.List[Error]
    
    
    @app.get(
        "/a",
    Python
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Fri Jun 30 18:25:16 GMT 2023
    - 3.5K bytes
    - Viewed (0)
  9. tests/test_response_code_no_body.py

    import typing
    
    from fastapi import FastAPI
    from fastapi.responses import JSONResponse
    from fastapi.testclient import TestClient
    from pydantic import BaseModel
    
    app = FastAPI()
    
    
    class JsonApiResponse(JSONResponse):
        media_type = "application/vnd.api+json"
    
    
    class Error(BaseModel):
        status: str
        title: str
    
    
    class JsonApiError(BaseModel):
        errors: typing.List[Error]
    
    
    @app.get(
        "/a",
    Python
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Fri Jun 30 18:25:16 GMT 2023
    - 3.2K bytes
    - Viewed (0)
  10. tests/test_additional_responses_custom_validationerror.py

    import typing
    
    from fastapi import FastAPI
    from fastapi.responses import JSONResponse
    from fastapi.testclient import TestClient
    from pydantic import BaseModel
    
    app = FastAPI()
    
    
    class JsonApiResponse(JSONResponse):
        media_type = "application/vnd.api+json"
    
    
    class Error(BaseModel):
        status: str
        title: str
    
    
    class JsonApiError(BaseModel):
        errors: typing.List[Error]
    
    
    @app.get(
        "/a/{id}",
    Python
    - Registered: Sun May 05 07:19:11 GMT 2024
    - Last Modified: Fri Jun 30 18:25:16 GMT 2023
    - 2.9K bytes
    - Viewed (0)
Back to top