Aman Sayeed
Aman Sayeed

Reputation: 41

TypeError: HTTPException() takes no keyword arguments

Error in terminal

raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"posts id {id} not found. " )
TypeError: HTTPException() takes no keyword arguments

My controller function is:

@app.get("/post/{id}")
async def get_post(id:int):
  data = get_post_by_id(id)
  
  if not data:
    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"posts id {id} not found. ")

  return{"data":data}

Upvotes: 3

Views: 3555

Answers (1)

MatsLindh
MatsLindh

Reputation: 52892

You're importing HTTPException from http.client - this is not the HTTPException class that FastAPI expects you to raise (or has documented). Instead import FastAPI's HTTPException class:

from fastapi import FastAPI, HTTPException, Body,, responses, status

Then raise should work as you expect.

Upvotes: 8

Related Questions