Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of HTTP PUT? [duplicate]

Why do we have method 'PUT' in AJAX and where is it most used?

Example:

$.ajax({
    url: 'script.php',
    type: 'PUT',
    success: function(response) {
        //...
    }
});

Why didn't the author simply use GET/POST instead?

like image 394
pixlboy Avatar asked Sep 15 '13 11:09

pixlboy


1 Answers

For RESTful APIs POST has a specific meaning (create a resource) while PUT has a different one (update an existing resource):

  • GET retrieves a list or an item
  • PUT replaces a collection or an item
  • POST creates a new item in a collection
  • DELETE deletes a collection or an item

However, if there's really "script.php" whoever developed it was not very thorough when creating his API. "script.php" is pretty much not RESTful at all... Usually the URL structure of a proper RESTful API looks e.g. like this:

  • http://example.com/questions would be a collection (GET to list, PUT to replace all items, POST to create a new item, DELETE to delete all items)
  • http://example.com/questions/123 would be an item (GET to retrieve, PUT to replace, POST usually unused, DELETE to delete that item)
like image 117
ThiefMaster Avatar answered Sep 27 '22 18:09

ThiefMaster