Can I use a PUT method in an HTML form to send data from the form to a server?
9 Answers
According to the HTML standard, you can not. The only valid values for the method attribute are get and post, corresponding to the GET and POST HTTP methods. <form method="put"> is invalid HTML and will be treated like <form>, i.e. send a GET request.
Instead, many frameworks simply use a POST parameter to tunnel the HTTP method:
<form method="post" ...> <input type="hidden" name="_method" value="put" /> ... Of course, this requires server-side unwrapping.
4XHTML 1.x forms only support GET and POST. GET and POST are the only allowed values for the "method" attribute.
1Can I use "Put" method in html form to send data from HTML Form to server?
Yes you can, but keep in mind that it will not result in a PUT but a GET request. If you use an invalid value for the method attribute of the <form> tag, the browser will use the default value get.
HTML forms (up to HTML version 4 (, 5 Draft) and XHTML 1) only support GET and POST as HTTP request methods. A workaround for this is to tunnel other methods through POST by using a hidden form field which is read by the server and the request dispatched accordingly. XHTML 2.0 once planned to support GET, POST, PUT and DELETE for forms, but it's going into XHTML5 of HTML5, which does not plan to support PUT. [update to]
You can alternatively offer a form, but instead of submitting it, create and fire a XMLHttpRequest using the PUT method with JavaScript.
4_method hidden field workaround
The following simple technique is used by a few web frameworks:
add a hidden
_methodparameter to any form that is not GET or POST:<input type="hidden" name="_method" value="PUT">This can be done automatically in frameworks through the HTML creation helper method.
fix the actual form method to POST (
<form method="post")processes
_methodon the server and do exactly as if that method had been sent instead of the actual POST
You can achieve this in:
- Rails:
form_tag - Laravel:
@method("PATCH")
Rationale / history of why it is not possible in pure HTML:
2Unfortunately, modern browsers do not provide native support for HTTP PUT requests. To work around this limitation, ensure your HTML form’s method attribute is “post”, then add a method override parameter to your HTML form like this:
<input type="hidden" name="_METHOD" value="PUT"/> To test your requests you can use "Postman" a google chrome extension
1for people using laravel
<form method="post" ...> @csrf @method('put') ... </form> 3If you are using nodejs, you can install the package method-override that lets you do this using a middleware. Link to documentation:
After installing this, all I had to do was the following:
var methodOverride = require('method-override') app.use(methodOverride('_method')) To set methods PUT and DELETE I perform as following:
<form method="PUT" action="domain/route/param?query=value" > <input type="hidden" name="delete_id" value="1" /> <input type="hidden" name="put_id" value="1" /> <input type="text" name="put_name" value="content_or_not" /> <div> <button name="update_data">Save changes</button> <button name="remove_data">Remove</button> </div> </form> <hr> <form method="DELETE" action="domain/route/param?query=value" > <input type="hidden" name="delete_id" value="1" /> <input type="text" name="delete_name" value="content_or_not" /> <button name="delete_data">Remove item</button> </form> Then JS acts to perform the desired methods:
<script> var putMethod = ( event ) => { // Prevent redirection of Form Click event.preventDefault(); var target = event.target; while ( target.tagName != "FORM" ) { target = target.parentElement; } // While the target is not te FORM tag, it looks for the parent element // The action attribute provides the request URL var url = target.getAttribute( "action" ); // Collect Form Data by prefix "put_" on name attribute var bodyForm = target.querySelectorAll( "[name^=put_]"); var body = {}; bodyForm.forEach( element => { // I used split to separate prefix from worth name attribute var nameArray = element.getAttribute( "name" ).split( "_" ); var name = nameArray[ nameArray.length - 1 ]; if ( element.tagName != "TEXTAREA" ) { var value = element.getAttribute( "value" ); } else { // if element is textarea, value attribute may return null or undefined var value = element.innerHTML; } // all elements with name="put_*" has value registered in body object body[ name ] = value; } ); var xhr = new XMLHttpRequest(); xhr.open( "PUT", url ); xhr.setRequestHeader( "Content-Type", "application/json" ); xhr.onload = () => { if ( xhr.status === 200 ) { // reload() uses cache, reload( true ) force no-cache. I reload the page to make "redirects normal effect" of HTML form when submit. You can manipulate DOM instead. location.reload( true ); } else { console.log( xhr.status, xhr.responseText ); } } xhr.send( body ); } var deleteMethod = ( event ) => { event.preventDefault(); var confirm = window.confirm( "Certeza em deletar este conteúdo?" ); if ( confirm ) { var target = event.target; while ( target.tagName != "FORM" ) { target = target.parentElement; } var url = target.getAttribute( "action" ); var xhr = new XMLHttpRequest(); xhr.open( "DELETE", url ); xhr.setRequestHeader( "Content-Type", "application/json" ); xhr.onload = () => { if ( xhr.status === 200 ) { location.reload( true ); console.log( xhr.responseText ); } else { console.log( xhr.status, xhr.responseText ); } } xhr.send(); } } </script> With these functions defined, I add a event listener to the buttons which make the form method request:
<script> document.querySelectorAll( "[name=update_data], [name=delete_data]" ).forEach( element => { var button = element; var form = element; while ( form.tagName != "FORM" ) { form = form.parentElement; } var method = form.getAttribute( "method" ); if ( method == "PUT" ) { button.addEventListener( "click", putMethod ); } if ( method == "DELETE" ) { button.addEventListener( "click", deleteMethod ); } } ); </script> And for the remove button on the PUT form:
<script> document.querySelectorAll( "[name=remove_data]" ).forEach( element => { var button = element; button.addEventListener( "click", deleteMethod ); </script> _ - - - - - - - - - - -
Beyond this, you can set postMethod function and getMethod to handle POST and GET submit methods as you like instead browser default behavior. You can do whatever you want instead use location.reload(), like show message of successful changes or successful deletion.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
I wrote an npm package called 'html-form-enhancer'. By dropping it into your HTML source, it takes over submission of forms with methods aside from GET and POST, and also adds application/json serialization.
<script type=module" src="html-form-enhancer.js"></script> <form method="PUT"> ... </form>