Home > Mobile >  API response of FastAPI delete call
API response of FastAPI delete call

Time:07-28

Is it necessary to return anything when a delete is successful, or does merely specifying the status code in the decorator do everything? For example:

@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id: int):
    cursor.execute("DELETE FROM posts WHERE id=%s", (id,))
    conn.commit()
    return Response(status_code=status.HTTP_204_NO_CONTENT)

Or, can I remote the return statement entirely?

@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id: int):
    cursor.execute("DELETE FROM posts WHERE id=%s", (id,))
    conn.commit()

CodePudding user response:

You can omit the return entirely, as the return code 204 is bound to not return any content. FastAPI replaces any content in 204, 304 and 1XX codes to b””.

  • Related