0

PugJS adding question mark in form action

form(method="DELETE", action='/shortener/'+ item._id)
    button.btn.btn-delete(type="submit") Delete

The URL I get:

http://localhost:8080/shortener/6421f534e7c2fbc3293e83ad?

I also tried to get the url using fetch()

public async destroy(req: Request, res: Response): Promise<Response> {
  await Shortener.findOneAndDelete({ _id: req.params.id });
  return res.status(204).json([]);
}
  • it's not pug: [Should PUT and DELETE be used in forms?](https://stackoverflow.com/a/73880506/4321299) – traynor Apr 11 '23 at 09:05

1 Answers1

1

DELETE is not a valid value for the method attribute, so the browser ignores it and reverts to the default (which is GET).

When you submit a GET form a query string on the action is added (or replaced if there is one there already) which consists of a ? followed by the key=value pairs of the form data (in this case there is no form data so you just get the ?).


If you want to make a DELETE request then you need to use JavaScript instead of a regular form submission.

If you want to make a form submission to tell the server to delete something, then use POST, not DELETE (as a side effect, POST won't add the query string).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335