Ahmad Anis
Ahmad Anis

Reputation: 2704

FastAPI {"detail":"Method Not Allowed"}

I am using FAST API for my ML Model.

I have a pipeline.

lr_tfidf = Pipeline([('vect', tfidf),
                     ('clf', LogisticRegression(penalty='l2'))])


Now In Fast API, when I want to predict, and display result as API, my code is

app = FastAPI()


@app.post('/predict')
def predict_species(data: str):
    data = np.array([data])

    prob = lr_tfidf.predict_proba(data).max()
    pred = lr_tfidf.predict(data)
    return {'Probability': f'{prob}', 
            'Predictions':f'{pred}'}

I copied it from a tutorial. When I test it on GUI by FASTAPI, it works good as shown in Image, i.e it shows probability and predictions.

enter image description here

When I go to request URL, as provided by the GUI, which is http://127.0.0.1:8000/predict?data=hello (test data is hello) It gives me error.

{"detail":"Method Not Allowed"}

On my Terminal, the error message is

INFO:     127.0.0.1:42568 - "GET /predict?data=hello HTTP/1.1" 405 Method Not Allowed

Upvotes: 30

Views: 83424

Answers (3)

David Miller
David Miller

Reputation: 517

I had to add the name of the route to local host path

@app.post("/api/chunking", tags=["chunking"])

http://localhost:{port}/api/chunking

Upvotes: 0

Towsif Ahamed Labib
Towsif Ahamed Labib

Reputation: 686

Using curl

Open a terminal or command prompt and run the following command:

curl -X POST http://127.0.0.1:8000/predict?data=hello

This command uses curl to make a POST request to the specified URL.

Using Python requests library

If you prefer using a Python script, first ensure you have the requests library installed. If not, you can install it using pip:

pip install requests

Then, you can use the following Python script to make the POST request:

import requests

url = "http://127.0.0.1:8000/predict"
params = {"data": "hello"}

response = requests.post(url, params=params)

print(response.text)

This script sends a POST request to the URL with the specified query parameters.

You can also use Postman, or create an api using JavaScript to perform a POST request

Upvotes: 3

finswimmer
finswimmer

Reputation: 15092

The method of the endpoint is defined as POST (@app.post('/predict')). When you call the URL from your browser, the HTTP Method is GET.

A simply solution is to change the endpoints method to GET via @app.get.

But this will most likely violates how REST-API endpoints should be named and when to use what HTTP method. A good starting point is https://restfulapi.net/resource-naming/.

Or maybe you are implementing an RPC (remote procedure call)? Than it can be different as well.

Upvotes: 33

Related Questions