Doing a HTTP PUT from a browser
Asked Answered
C

6

37

I would like to know what the definitive (?) answer is for how to do things other then POST/GET from a browser - either a HTML form or Ajax, as I hear mixed reports on what browsers allow what (specifically on the ajax side).

When building a back end in RESTful style it is nice to use proper verbs like PUT, HEAD, OPTIONS etc... in rails, a hidden form field called method (IIRC?) is used to simulate this, and at the back end the dispatch to the appropriate controller for the verb. Is this now (in late 2009) necessary? what are the conventions?

Chamfron answered 6/12, 2009 at 23:24 Comment(0)
D
39

It seems that most browsers don't support other methods besides GET and POST since it is a limitation of HTML forms. Here's another question on the topic:

Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

In order to simulate the PUT, DELETE, etc. methods, you could add a hidden input to a regular GET/POST form with the pseudo-method and have your application translate it so that your controllers see it as if it were a true PUT request, as you mentioned. I've seen this method used in google sitebricks (in java - sorry I don't have any rails-specific reference, but this might at least give you an idea) in this code. I think this is probably the method we are stuck with until something in HTML spec changes (and the browsers with it)

However, GET, POST, PUT and DELETE are supported in AJAX by the major browsers, so there should be no need for a hidden input if you aren't relying on the HTML form.

Diehl answered 6/12, 2009 at 23:49 Comment(6)
Nice - hopefully linking these together will help the google juice for others who look for things like this in future !Chamfron
I should also add, that I believe PUT and DELETE are supported according to HTML5: w3.org/TR/html5/forms.html#form-submission-0 (not sure about HEAD however).Diehl
I think AJAX is a lot better than the "tunneling" you suggest at first. Also, good point about HTML5. But I'm unclear on which browsers support PUT and DELETE for forms at this point.Pentameter
By looking at this page today, nearly 3 years later, it looks like w3 dropped PUT and DELETE again, or I miss it. But I can't find something else than GET and POST. So I guess HTML5 is no solution any more.Yip
Like @MichaelNeale states i'm Googling since yesterday evening trying to figure out if I should use PUT, PATCH and DELETE on my ASP.NET Web API. Major culprit here is the jQuery.ajax docs saying: "Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.". At this moment i'm still not sure if it's wise (thinking about browser support) to use other methods than GET and POST in my ajax 'type'-option.Micamicaela
I think it is safer these days than in years past.Chamfron
G
6

You have to use AJAX to do anything other than GETs and POSTs, I would recommend the jQuery Forms plugin to allow you to submit a form as a PUT.

Girlhood answered 7/12, 2009 at 0:29 Comment(0)
V
5

HTTP has 4 GET,POST,PUT,UPDATE. But most browser support only GET and POST. PUT and UPDATE are simulated by sending additional parameters in request. In rails it's _method="PUT" or _method="UPDATE".

Vitiate answered 7/12, 2009 at 0:32 Comment(1)
HTTP does not have an UPDATE method. However, as of 2010, it has PATCH. Also, there are more than 4 methods.Lotion
C
3

I believe the preferred solution to this problem is to use the X-HTTP-Method-Override header. If you search on this term you should find plenty of examples of how to use it.

Celinecelinka answered 7/12, 2009 at 2:24 Comment(0)
F
2

I think you'll find many firewalls block some of the cooler HTTP verbs. So while it may work for you, if you're trying to create something for the general public consumed from corporate sites, you'll probably want to stick with the basics.

Fairhaired answered 7/12, 2009 at 2:45 Comment(0)
D
1

Besides ajax generated requests, another way of getting these additional methods is with a webdav client. filesystem clients exist for all major operationg systems, and there are some additional clients that can support it for web authoring.

For instance, the Amaya web browser allows you to edit documents on the web and save them directly to the server, using the PUT method. There are plugins for this on other browsers and several web graphical editors, such as Dreamweaver also support WebDAV.

WebDAV also supports a number of other methods besides the methods defined in HTTP1.1 for its own use.

Disabled answered 7/12, 2009 at 3:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.