Using Put Method in HTML Form

Using PUT method in HTML form

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

HTML form PUT method

Browsers only support POST and GET, if you need PUT, you have to send the form via post/get and then do the proper PUT request on server-side.

EDIT although, most implementations of XMLHttpRequest support PUT and DELETE.

How to use PUT method in HTML/PHP Form?

Unfortunatly HTML forms don't support the PUT method, rather than submitting the form directly to the third party, you could POST to another PHP script on your server, and then do the PUT request from your server, something like:

$data = array("jumlah" => $_POST["jumlah"]);
$ch = curl_init($this->_serviceUrl . $id);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

curl_exec($ch);

Laravel form html with PUT method for PUT routes

You CAN add css clases, and any type of attributes you need to blade template, try this:

{{ Form::open(array('url' => '/', 'method' => 'PUT', 'class'=>'col-md-12')) }}
.... wathever code here
{{ Form::close() }}

If you dont want to go the blade way you can add a hidden input. This is the form Laravel does, any way:

Note: Since HTML forms only support POST and GET, PUT and DELETE
methods will be spoofed by automatically adding a _method hidden field
to your form. (Laravel docs)

<form class="col-md-12" action="<?php echo URL::to('/');?>/post/<?=$post->postID?>" method="POST">

<!-- Rendered blade HTML form use this hidden. Dont forget to put the form method to POST -->

<input name="_method" type="hidden" value="PUT">

<div class="form-group">
<textarea type="text" class="form-control input-lg" placeholder="Text Here" name="post"><?=$post->post?></textarea>
</div>

<div class="form-group">
<button class="btn btn-primary btn-lg btn-block" type="submit" value="Edit">Edit</button>
</div>
</form>

Why submitting form with PUT method I got GET request?

HTML Forms only support GET and POST.

From the docs:

Since HTML forms can't make PUT, PATCH, or DELETE requests, you will
need to add a hidden _method field to spoof these HTTP verbs.

You can use the method_field helper or the @method blade directive to add the hidden input.

<form action="/foo/bar" method="POST">
@method('PUT')
...
</form>

or

<form action="/foo/bar" method="POST">
{{ method_field('PUT') }}
...
</form>

Http Put is no being sent from a razor page

HTML forms don't support PUT, DELETE, etc.: only GET and POST. If you need to send a PUT, you'll need to use AJAX to do so.

Form data to rest PUT method in java

As mentioned by many in Using PUT method in HTML form, PUT is not currently supported by the HTML standard. What most frameworks will do is offer a workaround. Jersey has such a workaround with its HttpMethodOverrideFilter. What you must do is use a POST method and add a _method=put query parameter and the filter will switch the POST to a PUT.

You first need to register the filter. If you are using a ResourceConfig just do

@ApplicationPath("api")
public class JerseyConfig extends ResourceConfig {

public JerseyConfig() {
...
register(HttpMethodOverrideFilter.class);
}
}

If you are using a web.xml, then do

<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.server.filter.HttpMethodOverrideFilter</param-value>
</init-param>

Then in your HTML, you will just add the _method=put query param to the URL. Below is an example I used to test

<form method="post" action="/api/form?_method=put">
<label>
Name:
<input type="text" name="name"/>
</label>
<label>
Age:
<input type="number" name="age"/>
</label>
<br/>
<input type="submit" value="Submit"/>
</form>

And in your resource method you will use @PUT and @FormParams for the paramters

@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response form(@FormParam("name") String name,
@FormParam("age") String age,
@Context UriInfo uriInfo) {

URI redirectUri = UriBuilder
.fromUri(getBaseUriWithoutApiRoot(uriInfo))
.path("redirect.html")
.queryParam("name", name)
.queryParam("age", age)
.build();
return Response.temporaryRedirect(redirectUri).build();
}

private static URI getBaseUriWithoutApiRoot(UriInfo uriInfo) {
String baseUri = uriInfo.getBaseUri().toASCIIString();
baseUri = baseUri.endsWith("/")
? baseUri.substring(0, baseUri.length() - 1)
: baseUri;
return URI.create(baseUri.substring(0, baseUri.lastIndexOf("/")));
}

It should work from what I tested



Related Topics



Leave a reply



Submit