Can I use a PUT method in an HTML form to send data from the form to a server?
问题:
回答1:
XHTML 1.x forms only support GET and POST. GET and POST are the only allowed values for the \"method\" attribute.
回答2:
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.
回答3:
Can 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
This method is used by Rails, and could be adapted to any framework / project:
add a hidden
_method
parameter 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 (e.g. Rails
form_tag
)fix the actual form method to POST (
<form method=\"post\"
)processes
_method
on the server and do exactly as if that method had been sent instead of the actual POST
Rationale / history of why it is not possible: https://softwareengineering.stackexchange.com/questions/114156/why-there-are-no-put-and-delete-methods-in-html-forms
回答5:
Unfortunately, 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