Method put in fastapi jinja2 not working/ 405 Method not allowed


I am having problem with Fastapi, Jinja2 put method. My task is to save form, here is ajax in html file

    function submitForm() {
        var url = 'http://localhost:8000';
        let id = document.getElementById("person-id").value;
        var data = {id: id};
        fetch(url, {
          method: 'PUT',
          body: JSON.stringify(data),
            'Content-Type': 'application/json'
        }).then(res => res.json())
        .then(response => console.log('Success:', JSON.stringify(response)))
        .catch(error => console.log('Error:', error));

Here py method update

@router.put("/{id}", response_class=Response)
async def update(request: Request, id: str, req: UpdateModel = Body(...)):
   updated = await update(id, req.dict())
   return templates.TemplateResponse('index.html', context={'request': request})

Update is not working, console is just showing
405 method not allowed

Hey there @jypara ! Let me see what can be done about this. I’m not completely familiar with FastAPI, but @router.put("/{id}", response_class=Response) leads me to believe that the server is expecting the URL like this: http://localhost:8000/123 where 123 is the ID parameter. You, however, in your call just make a request to http://localhost:8000/, which isn’t the same route. I suspect the / route either doesn’t exist or doesn’t allow PUT requests.

In your JavaScript, try replacing your definition of url with this:

var url = `http://localhost:8000/${id}`

and report back if anything is different now!