Getting "Method Not Allowed" when using fastapi

Question:

I am trying to run the following code but I am getting a {"detail":"Method Not Allowed"} error when I open http://localhost:8000/predict.

The code:

from fastapi import FastAPI

app = FastAPI()

@app.post("/predict")
def predict_(request: str):
    return {"message": 'Hellooo!'}

What’s the problem with my code?

I searched online for similar examples but I got the same error! For example this one: https://codehandbook.org/post-data-fastapi/

Asked By: Minions

||

Answers:

It’s because when you open the page in a browser, it makes a GET request, but your app only handles POST. Change to @app.get(...), and you should be able to see it in a browser. Or, navigate to http://localhost/docs and use the interactive documentation to test it as it is.

Answered By: M.O.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.