Using PUT method in HTML form

243,970

Solution 1

XHTML 1.x forms only support GET and POST. GET and POST are the only allowed values for the "method" attribute.

Solution 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.

Solution 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.

Solution 4

_method hidden field workaround

The following simple technique is used by a few web frameworks:

  • 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.

  • 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

You can achieve this in:

  • Rails: form_tag
  • Laravel: @method("PATCH")

Rationale / history of why it is not possible in pure HTML: https://softwareengineering.stackexchange.com/questions/114156/why-there-are-no-put-and-delete-methods-in-html-forms

Solution 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

Share:
243,970

Related videos on Youtube

WelcomeTo
Author by

WelcomeTo

Updated on July 08, 2022

Comments

  • WelcomeTo
    WelcomeTo almost 2 years

    Can I use a PUT method in an HTML form to send data from the form to a server?

  • hakre
    hakre over 12 years
    You have been linking a draft, not a final standard. Just noting, the stable HTML versions don't offer it either.
  • phihag
    phihag over 12 years
    @hakre HTML5 is the de-facto standard already, and will probably evolve over time. What the W3C calls "Draft" is a documented developed over at least 3 years with the input of browser vendors with more than >99%, and has already been implemented (at least when it comes to this and most non-esoteric sections) by all of them forever. But just for nitpickers, here's the equivalent definition in HTML 4.01 (a Technical Request in the W3C's terms).
  • hakre
    hakre over 12 years
    Please don't feel offended, I already wrote that it's just a note and that the other HTML versions don't offer it either (with a slight exception of XHTML 2, but that's an obsolete draft now).
  • phihag
    phihag over 12 years
    @hakre Rest assured I'm not offended at all. Nitpicking the nitpicker, I must comment that XHTML is technically not HTML, although one might find one or two similarities ;)
  • Eugen Konkov
    Eugen Konkov about 7 years
  • Tofeeq
    Tofeeq about 7 years
    It should work for any method including DELETE AND PATCH etc
  • jakubiszon
    jakubiszon over 6 years
    XHTML is somewhat outdated. Still it looks like same two values are valid in html5 developer.mozilla.org/en-US/docs/Web/HTML/Element/…
  • JacopoStanchi
    JacopoStanchi almost 6 years
    An AJAX request cannot replace completely a form request because a form request redirects the user to the given resource. For example there is no way as of right now to show in the browser the page of the route PUT /resource.
  • DaBlick
    DaBlick over 5 years
    It's a bad idea to do this. Frameworks can ignore form parameters for PUTs. Java's HTTPServlet seems to. We had a bug where HttpRequest.getParameterMap() did not return form parameters.
  • hakre
    hakre over 5 years
    @DaBlick: But not for XMLHttpRequests? If so then relying on the fetch API should be more standardized.
  • santiago arizti
    santiago arizti almost 5 years
    Laravel (php) has the same feature by using @method("PATCH")
  • code_dredd
    code_dredd almost 5 years
    It seems like a reasonable workaround, but still unfortunate, since it makes debugging difficult. For example, if you need to go through server logs or do network analysis to verify some things, you won't see correct output, since the HTTP method being used in the HTTP headers is still POST and not what you actually needed.
  • Rayees AC
    Rayees AC over 3 years
    The code is not only solution. Please add description too. Thanks.
  • CodeToLife
    CodeToLife over 3 years
    @Rayees AC, this is a solution for them who does work in Laravel framework, fyi. Had written that <b>bolded</b> on the top of the post. Didnt get what youre about otherwise
  • MemeDeveloper
    MemeDeveloper over 3 years
    Although SO sensibly encourages people not to post code only answers, sometimes... like this case... it's all you need.