How to specify DELETE method in a link or form?
Asked Answered
L

9

47

Rfc2616 lists many methods besides GET and POST, like, say, DELETE, PUT etc. Method field in html forms, though, seems to be allowed to specify only GET or POST.

Is it possible to create a link or form in a html page that uses a request method that is not GET or POST?

Llama answered 3/8, 2011 at 12:33 Comment(2)
See #166279Senecal
After following suggested links, I believe the definitive answer would be: using anything but GET and POST is not possible from an html page without scripting. Although html5 allowed PUT and DELETE in forms for a while, those were removed.Brethren
I
21

Was trying to figure this out for a rails app that was using Angular on the front end; these seems to work for that environment:

<a data-confirm="Are you sure?" data-method="delete" href="/link-to-resource" rel="nofollow">Delete</a>

Edit: Just to give everyone a heads up, I think you still need to have jQuery for this to work. I removed jQuery and it stopped working; I put it back and it started working.

Inundate answered 9/2, 2016 at 3:2 Comment(3)
This behavior comes from jquery_ujs, which is part of jquery-rails. Unobtrusive Java Script. It's what also does the data:{confirm: 'do you?'} behavior.Coset
how can i achieve the same in rails reactCryoscopy
@OshanWisumperuma try using something like dataMethod and dataConfirm for props. Haven't used React on Rails, but if I recall correctly, React uses camelCased props for certain HTML properties: reactjs.org/docs/dom-elements.html You will probably need to have jquery_ujs for this to work; however, it would also be trivial to use fetch and pass up an HTTP DELETE request.Inundate
T
18

You certainly can’t create a link that uses anything other than GET. Since HTML began, links have been meant to be idempotent and free from side effects.

For forms and XMLHTTPRequests, Caps’ link is the place to look: Are the PUT, DELETE, HEAD, etc methods available in most web browsers?.

Transference answered 3/8, 2011 at 13:41 Comment(5)
the only problem that my links with data-method attrib (comming from rails,was an error ..) are POSTing! at least with FF, Chrome and IE ...Lytic
@halfbit: that’s due to some JavaScript that Rails includes. I’d humbly suggest it’s a terrible idea to make links do anything other than an HTTP GET, especially with JavaScript.Transference
oh, yes, I found that out allready (shame on me). I forgot to remove my comment, because false. sry againLytic
Then again most of the world clicks on GET links for verification of user accounts in emails.Reversal
@Jonny: I’m not clear what that’s got to do with my answer.Transference
B
18

By default, not there is no way to do this. Links always perform GETs, forms can use GETs or POSTs.

That said, with a little JavaScript, it's possible. Rails for instance ships with helpers which will add a data-method attribute to links. Rails-UJS is a jQuery library that will transparently intercept clicks on these links, and trigger a form submit with a _method parameter used for overriding the normal HTTP method. Finally Rack will intercept requests with a _method params, and overwrite the request method with the value in _method.

Other frameworks no doubt follow a similar pattern.

If you want even more details, I've written up an explanation of how Rails, Rails-UJS, and Rack all work together to provide this.

It's good to know how your libraries work.

Basanite answered 17/5, 2016 at 17:7 Comment(2)
Your link goes to a localhost website. Could you link to the online article?Alfano
monkeyandcrow.com/blog/reading_rails_http_delete_with_a_linkIbsen
E
4

It is not possible to create a link or form with delete method.

Many web framework create a hidden input called "_method" for handling PUT and DELETE.

I created a plugin for automatically convert links to forms : RestfulizerJs

You can take a look here : https://github.com/Ifnot/RestfulizerJs

Edmonds answered 19/2, 2014 at 14:28 Comment(0)
P
1

@Ifnot plugin is great but I created a one based on $.ajax function instead of appending hidden forms! here's a simple example for a DELETE request

HTML

<button class="delete" data-target="http://example.com/post/post-id/" data-method="DELETE" data-disabled="true">Delete Article</button>

JavaScript

$(".delete").restintag(optionsObj, function(data) {
    console.log(data);
},
function(error) {
    console.log(error);
});

https://github.com/KhaledElAnsari/RESTInTag/

Pierce answered 5/3, 2017 at 11:7 Comment(0)
H
1

HTMX is designed to solve this.

Why should only <a> and <form> be able to make HTTP requests? Why should only click & submit events trigger them? Why should only GET & POST methods be available? Why should you only be able to replace the entire screen? By removing these arbitrary constraints, htmx completes HTML as a hypertext

It enables any DOM element to make HTTP requests GET, POST, PUT, PATCH, and DELETE, without writing any custom JavaScript.

Hygienic answered 5/2, 2023 at 9:5 Comment(0)
A
1

Here is a straight javascript way to do it.

<body>
<p><a href="javascript: deleteApp('app1')">Delete app1</a></p>
<script>
function deleteApp(branch) {
  fetch('https://example.com/api/v1/resource/'+branch, {
    method: 'DELETE',
    headers: {
      'Authorization': 'Bearer <your_access_token>'
    }
  });
}
</script>
</body>
Aerobiology answered 29/8, 2023 at 13:47 Comment(0)
C
0

if you use rails then in application.js you should add this line. Because a link has only GET method awailable

//= require rails-ujs

then you can use any CRUD methods

examples for DELETE

<a href="path/to/delete" data-method="delete">delete some post</a>

or

<%= link_to "delete some post", post_path, method: :delete %>
Canter answered 26/1 at 13:53 Comment(0)
C
-9

just add a data-method attribute,<a data-method='put' href='whatever'>link</a>

Cotyledon answered 5/12, 2013 at 19:3 Comment(4)
Can you please link to any kind of documentation that this is correct?Keheley
Downvoted because this answer is very vague and based on the information given, it's incorrect. I'm not sure how it achieved the highest number of upvotes.Egerton
Maybe it is vague, but indeed it is the correct one, and should receive up-votes. For the question above, it could be used like this: <a rel="nofollow" data-method="delete" href="some_link">DeleteMe</a>Chronometer
No this is a Rails/jQuery ism. It is not proper protocol. anchor links are get only. This is another example of magic being done that people are not understanding that it is NOT vanilla HTMLJugal

© 2022 - 2024 — McMap. All rights reserved.