Home / Function/ get_request_handler() — fastapi Function Reference

get_request_handler() — fastapi Function Reference

Architecture documentation for the get_request_handler() function in routing.py from the fastapi codebase.

Function python FastAPI Routing calls 7 called by 1

Entity Profile

Dependency Diagram

graph TD
  ad8d62d9_53da_514b_c06f_53293eb1e5ae["get_request_handler()"]
  de395a51_26f8_3424_1af0_2f5bef39c893["routing.py"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|defined in| de395a51_26f8_3424_1af0_2f5bef39c893
  2b3c8ec5_f007_7808_7e6a_0e94569b6a17["get_route_handler()"]
  2b3c8ec5_f007_7808_7e6a_0e94569b6a17 -->|calls| ad8d62d9_53da_514b_c06f_53293eb1e5ae
  28261a63_2ba9_f1ff_1d1b_475348a45a65["Default()"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|calls| 28261a63_2ba9_f1ff_1d1b_475348a45a65
  1b892f95_174f_a6fa_5ff9_9cd8b88e8ee0["get()"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|calls| 1b892f95_174f_a6fa_5ff9_9cd8b88e8ee0
  74570640_d48f_6978_d994_fcf56400f5f3["_extract_endpoint_context()"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|calls| 74570640_d48f_6978_d994_fcf56400f5f3
  fc3220b9_b8bc_99f6_605f_911723c78183["solve_dependencies()"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|calls| fc3220b9_b8bc_99f6_605f_911723c78183
  0f69147d_dc63_05c7_c9b3_7b83bf622c75["run_endpoint_function()"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|calls| 0f69147d_dc63_05c7_c9b3_7b83bf622c75
  ef417485_6bd2_0f62_cc05_f62f181d92fc["serialize_response()"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|calls| ef417485_6bd2_0f62_cc05_f62f181d92fc
  dba5a7c0_1fab_8f88_2c80_2b79c3b3dc47["is_body_allowed_for_status_code()"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|calls| dba5a7c0_1fab_8f88_2c80_2b79c3b3dc47
  style ad8d62d9_53da_514b_c06f_53293eb1e5ae fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/routing.py lines 319–474

def get_request_handler(
    dependant: Dependant,
    body_field: Optional[ModelField] = None,
    status_code: Optional[int] = None,
    response_class: Union[type[Response], DefaultPlaceholder] = Default(JSONResponse),
    response_field: Optional[ModelField] = None,
    response_model_include: Optional[IncEx] = None,
    response_model_exclude: Optional[IncEx] = None,
    response_model_by_alias: bool = True,
    response_model_exclude_unset: bool = False,
    response_model_exclude_defaults: bool = False,
    response_model_exclude_none: bool = False,
    dependency_overrides_provider: Optional[Any] = None,
    embed_body_fields: bool = False,
) -> Callable[[Request], Coroutine[Any, Any, Response]]:
    assert dependant.call is not None, "dependant.call must be a function"
    is_coroutine = dependant.is_coroutine_callable
    is_body_form = body_field and isinstance(body_field.field_info, params.Form)
    if isinstance(response_class, DefaultPlaceholder):
        actual_response_class: type[Response] = response_class.value
    else:
        actual_response_class = response_class

    async def app(request: Request) -> Response:
        response: Union[Response, None] = None
        file_stack = request.scope.get("fastapi_middleware_astack")
        assert isinstance(file_stack, AsyncExitStack), (
            "fastapi_middleware_astack not found in request scope"
        )

        # Extract endpoint context for error messages
        endpoint_ctx = (
            _extract_endpoint_context(dependant.call)
            if dependant.call
            else EndpointContext()
        )

        if dependant.path:
            # For mounted sub-apps, include the mount path prefix
            mount_path = request.scope.get("root_path", "").rstrip("/")
            endpoint_ctx["path"] = f"{request.method} {mount_path}{dependant.path}"

        # Read body and auto-close files
        try:
            body: Any = None
            if body_field:
                if is_body_form:
                    body = await request.form()
                    file_stack.push_async_callback(body.close)
                else:
                    body_bytes = await request.body()
                    if body_bytes:
                        json_body: Any = Undefined
                        content_type_value = request.headers.get("content-type")
                        if not content_type_value:
                            json_body = await request.json()
                        else:
                            message = email.message.Message()
                            message["content-type"] = content_type_value
                            if message.get_content_maintype() == "application":
                                subtype = message.get_content_subtype()
                                if subtype == "json" or subtype.endswith("+json"):
                                    json_body = await request.json()
                        if json_body != Undefined:
                            body = json_body
                        else:
                            body = body_bytes
        except json.JSONDecodeError as e:
            validation_error = RequestValidationError(
                [
                    {
                        "type": "json_invalid",
                        "loc": ("body", e.pos),
                        "msg": "JSON decode error",
                        "input": {},
                        "ctx": {"error": e.msg},
                    }
                ],
                body=e.doc,
                endpoint_ctx=endpoint_ctx,
            )

Domain

Subdomains

Defined In

Frequently Asked Questions

What does get_request_handler() do?
get_request_handler() is a function in the fastapi codebase, defined in fastapi/routing.py.
Where is get_request_handler() defined?
get_request_handler() is defined in fastapi/routing.py at line 319.
What does get_request_handler() call?
get_request_handler() calls 7 function(s): Default, _extract_endpoint_context, get, is_body_allowed_for_status_code, run_endpoint_function, serialize_response, solve_dependencies.
What calls get_request_handler()?
get_request_handler() is called by 1 function(s): get_route_handler.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free