FastAPI from entry to actual combat (15) - setting general routing

This article mainly records how fastapi implements one interface and multiple request methods; for example, the same interface can request data through query parameters or path parameters, and can also get a response by sending the request body.

Common Request Methods

  1. Get sends a request to a specific resource (the request specifies page information and returns the entity body)
  2. Post submits data to the specified resource for processing requests (submitting forms, uploading files), which may lead to the establishment of new resources or the modification of original resources
  3. Put uploads its latest content to the specified resource location (the data transmitted from the client to the server replaces the content of the specified document)
  4. Head is consistent with the server request and the get request, the response body will not be returned, and the original information contained in the small message header is obtained (similar to the get request, there is no specific content in the returned response, which is used to obtain the header)
  5. Delete Request the server to delete the resource indicated by the request-URL* (request the server to delete the page)
  6. Trace echoes requests received by the server for testing and diagnostics
  7. opions Returns the HTML request method supported by the server for a specific resource or the web server sends *test server functionality (allows the client to view server performance)
  8. A proxy server in the Connect HTTP/1.1 protocol that can change the connection to a pipeline
  • The get request has no message body, can only carry a small amount of data, and is not safe; the get request puts the data in the url address
  • The post request has a message body, which can carry a large amount of data and is safe; the post request puts the data in the message body
  • The data submitted by GET method can only have a maximum of 1024 bytes, while POST does not have this limitation.

FastAPI setting general routing

The writing interface in FastAPI is @app.methodsimplemented through the method, which limits the request method to a certain type, which cannot meet our development needs in some scenarios, so it is necessary to implement multiple request methods for the same route. @appThe and here methodsare all references, and it is not written on the official website. After reading github and the source code, I roughly figured it out;

source code

It can be seen from here that the above implementation method also depends on api_routethe method, so api_routeit can be realized directly by using the method.

 def get(
        self,
        path: str,
        *,
        ......
    ) -> Callable[[DecoratedCallable], DecoratedCallable]:
        return self.api_route(
            path=path,
            response_model=response_model,
            status_code=status_code,
            tags=tags,
            dependencies=dependencies,
            summary=summary,
            description=description,
            response_description=response_description,
            responses=responses,
            deprecated=deprecated,
            methods=["GET"],
            operation_id=operation_id,
            ......
        )

Code

@app.api_route("/test", methods=["GET", "POST", "DELETE"])
async def test(request: Request):
    return {
    
    "method": request.method}

As above, the method is very simple. Use api_routethe method, set the path, and set methodsthe list at the same time. The documents and tests are as follows:

image-20221215113834317

image-20221215113856955

image-20221215113918562

image-20221215113937127


Thanks for reading!

Jiumozhai Address: https://blog.jiumoz.com/archives/fastapi-cong-ru-men-dao-shi-zhan-15-she-zhi-tong-yong-lu-you

Welcome to follow the blogger's personal mini program!

Guess you like

Origin blog.csdn.net/qq_45730223/article/details/128327968