'405 method not allowed when using 'put', but success with 'get', why?

I'm learning FastAPI from the official documentation.

When I try running the first example from https://fastapi.tiangolo.com/tutorial/body-multiple-params/ and pasted the URL http://127.0.0.1:8000/items/35 in the browser, my server sends a message

405 Method not allowed

Example code from the link is like below,

from typing import Optional

from fastapi import FastAPI, Path
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None


@app.put("/items/{item_id}")
async def update_item(
    *,
    item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
    q: Optional[str] = None,
    item: Optional[Item] = None,
):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    if item:
        results.update({"item": item})
    return results

I understand q and item parameters are optional in this example, so think it can respond with only item_id variable, but it fails.

But if I changed the method to get, which means modified the code with @app.put("/items/{item_id}"), it works.

I want to know what makes this difference.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source