Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing a HTTP PUT from a browser

Tags:

rest

http

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?

like image 764
Michael Neale Avatar asked Dec 06 '09 23:12

Michael Neale


People also ask

How do I send a HTTP PUT request?

Sending a PUT Request with Axios The simplest way to make the PUT call is to simply use the put() function of the axios instance, and supply the body of that request in the form of a JavaScript object: const res = await axios. put('/api/article/123', { title: 'Making PUT Requests with Axios', status: 'published' });

Which is the only method of HTTP used to pass payload?

The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload.

Can you send body put request?

So yes, a PUT request, technically, strictly, has to have a body.


1 Answers

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.

like image 107
mpobrien Avatar answered Sep 18 '22 20:09

mpobrien