Is it considered bad practice to perform HTTP POST without entity body?

后端 未结 6 873
粉色の甜心
粉色の甜心 2020-12-12 13:58

I need to invoke a process which doesn\'t require any input from the user, just a trigger. I plan to use POST /uri without a body to trigger the process. I want to know if t

6条回答
  •  温柔的废话
    2020-12-12 14:41

    Support for the answers that POST is OK in this case is that in Python's case, the OpenAPI framework "FastAPI" generates a Swagger GUI (see image) that doesn't contain a Body section when a method (see example below) doesn't have a parameter to accept a body.

    the method "post_disable_db" just accepts a path parameter "db_name" and doesn't have a 2nd parameter which would imply a mandatory body.

    @router.post('/{db_name}/disable',
                 status_code=HTTP_200_OK,
                 response_model=ResponseSuccess,
                 summary='',
                 description=''
                 )
    async def post_disable_db(db_name: str):
        try:
            response: ResponseSuccess = Handlers.databases_handler.post_change_db_enabled_state(db_name, False)
        except HTTPException as e:
            raise (e)
        except Exception as e:
            logger.exception(f'Changing state of DB to enabled=False failed due to: {e.__repr__()}')
            raise HTTPException(HTTP_500_INTERNAL_SERVER_ERROR, detail=e.__repr__())
    
        return response
    

提交回复
热议问题