How to use other HTTP methods in HTML forms?

2

I'm using Express.JS to do a CRUD on NodeJS.

I was trying to make use of the new HTTP methods, such as put , but when I put it in the method attribute of the form it does not seem to work.

The form code:

[...]

<form action="/post/content" method="put">

[...]

How do I make it work?

Part of the backend code:

app.put('/post/content', (req, res) => {
  console.log(req.body);
});
    
asked by anonymous 25.02.2018 / 16:08

1 answer

2

The solution

The PUT method does not exist in HTML standards . The DELETE also does not.

It's common to use this workaround :

<form method="post">
  <input type="hidden" name="_method" value="put" />
  ...
</form>

Or make a XMLHttpRequest with PUT in client-side JavaScript.

Why are there no PUT and DELTE methods in HTML forms?

HTML5 sketches were considered to use the PUT and DELETE methods and were even implemented in the Firefox browser in its beta version.

Consider PUT and DELETE support as method of form was discussed in W3C and was closed as "resolved, but will not be fixed" for the following reason (translation and emphasis mine):

  

PUT as a method of form does not make any sense. You would not want a PUT in the payload of a form. DELETE only makes sense when there is payload , so it does not make sense in forms either.

On the day I write this, in 2018, HTML forms only support GET and POST.

    
25.02.2018 / 16:12