Minions
Minions

Reputation: 5467

Getting "Method Not Allowed" when using fastapi

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/

Upvotes: 0

Views: 831

Answers (1)

M.O.
M.O.

Reputation: 2911

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.

Upvotes: 1

Related Questions