What is the difference between PUT and POST?

Asked

Viewed 52,318 times

31

Some teach that POST is to send data to create something and that PUT is to update, but found poorly explained.

So, after all, what is the difference between the PUT method and the POST?

When I must wear one and the other?

  • 1

    I think it’s more conceptual, but I’ve always wanted to know that, too.

  • 1

    When someone commented on these definitions, they said something about Rest?

  • The difference between the two is explained in the following link: Method Definitions - PUT (I’m too lazy to translate the third paragraph of this section).

  • Related question (not answering the question): http://answall.com/q/49322

  • 4

    I’m waiting for someone to put an answer to that beautiful word ;)

4 answers

32


Technically none. Only semantics change. Read the RFC:

The fundamental difference between POST and PUT requests is reflected in the difference in meaning of the Request-URI. The URI on a POST request identifies the resource that will manipulate the appended entity. The resource may be a data acceptance process, a gateway for another protocol, or a separate entity that accepts annotations. In contrast, the URI in the PUT request identifies the appended entity with the request -- the user agent knows which URI is intended and the server MUST NOT attempt to apply the request to another resource. If the server wants the request to be applied to a different URI, it MUST send a 301 (Moved Permanently) response; user agent You can then make your own decision on whether or not to redirect the request.

In other words, PUT should be used in more specific situations where you want to send information and do nothing but store it in some way. The POST is more general, it assumes that information is only part of the process that will be carried out. The POST allows to have any side effects, or at least it is expected that this can occur. PUT is expected not to have something unknown, or undesirable. It is idempotent (homage to the bfavaretto).

In some scenarios it is possible to think of more specific semantics (REST, I’m talking about you) for each verb. But essentially they work similarly. In CRUD scenarios, the PUT normally is used to update something and the POST to create (or do other operations that does not fit into the basic CRUD), just as GET is used to read, and DELETE obviously to delete.

Of course, the recommendation is to follow what the RFC says, but it’s almost safe to say it doesn’t matter much. In the future it could be that they impose some restriction or have some effect based on what the RFC says. And then anything that didn’t follow her would have problems. " Legally" they could do this, but since no one follows the RFC, it would break so much, that it will never be changed.

When in doubt, everyone uses the POST that is better known and should always work. I have my doubts if it should not be the other way around. But the damage is already done.

Although it does not answer directly, I strongly advise to read the response indicated by bfavaretto. Has another useful also.

18

The post is a verb HTTP for data submission within the package on any type of request, very similar to get, changing only the location and size available for data storage. Against put is a verb that carries the meaning of creating elements, such as sending a file or entity.

Update 1

When requesting a resource, such as a query, it is usual to use the information passage using get.

URI: http://servidor/recurso/consulta/{id}
GET: {id}

However, use the verb get generates URI's individual values for each given value, exposing the information and preventing cache optimization if the request response is the same. In this way we have the verb post to load the information inside the package without being in the URI.

URI: http://servidor/recurso/consulta
POST: {id}

The verbs put and delete are loaded with semantics. When creating or transferring a resource the most appropriate verb is put. To exclude a resource the most appropriate verb is the delete.

URI: http://servidor/recurso/novo
PUT: {nome,cidade,estado}

URI: http://servidor/recurso/excluir/{id}
DELETE
  • 1

    Guys, if he’s wrong on the answer, I’d like to know why at least! I’m a layman on this subject!

  • +1 because I didn’t think -1 would be fair. From what I’ve seen he responded quickly and with his own words, and the context sounds like some answers out there that I’ve seen are copies of posts on the net.

  • 1

    These negative people are burning the community movie! The people know negatively, but they don’t know how to give feedbacks.

  • 3

    +1 for it is not an answer of all evil, however in proximas answers recommend providing details or a source, this helping your answer has a higher quality, Not that she hasn’t.

  • 1

    Good evening @Mfedatto your reply is being discussed here if you want to participate welcome: http://meta.pt.stackoverflow.com/q/4446/3635 O meta it’s a nice place to argue and try to work it out, so don’t take it personally if something pops something you don’t like :)

  • Oops, @Guilhermenascimento! I deactivated the email notifications because I’m always stopping by during the week. I understand perfectly not getting +1 from anyone who thinks the answer would be more complete, but I believe my answer is correct and answers the initial question, so I see no reason for -1. Anyway, I’ll give you some examples.

  • @Mfedatto this is being discussed at the finish line, feel free to talk about it there. I think my answer should make it very clear how to avoid such vows, whether they seem right or not.

  • 1

    The answer is not all bad, the problem is more in the application "verbal", define the meaning of a request to the grammatical meaning "verb" is a bad definition of the behavior of it. Although the "words" put, get and delete are action verbs, their behavior is not a verb but a request. But I will not deny it for a simple detail of this.

  • @Ivanferrer verb is the appropriate technical term. POST, GET, PUT and DELETE are verbs HTTP.

  • I agree that they are HTTP "verbs", which is precisely the difference between the meaning of "parameters", but I particularly do not like to see someone using verb employment as a technical term. I would keep this in Portuguese, which is where "verb" has the most meaning and meaning, even if we have to use verbs to represent the calls.

  • It seems that someone murdered the grammatical language and appropriated its use to embroider meaningless technical thermology, probably what damaged the sense, was the translation into Portuguese of "verbose" and the attribution to the property giving another meaning and meaning. But anyway, it’s just an opinion. That’s why I didn’t deny.

  • It is the translation for "Verb". Verbose translates verbosity. In the specific literature you find http verbs and verbos http.

Show 7 more comments

15

PUT places a file or resource in a URI specific. If a file or resource already exists in this URI, the method PUT will replace the file (or resource). If there is no file or resource there, the method PUT creates a.

POST sends data to a URI specific to handle the request. The server web, at this point can determine what to do with the data in the context of the specified resource.

source (English)

  • a revision in the text would be nice.. complicated understand because it seems that just translated with google Translate and played here. By the way, just to confirm, I copied the originals and played on Translate... It was exactly the same ... with the same grammatical errors.

  • @Danielomine was exactly that! I copied the text and when I was going to change appeared an urgent task here, and I had to leave for another time, rs.

1

PUT is an idempotent operation to "place" (CREATE OR REPLACE) a resource at a given URL, in other words, it is used when the URL uniquely identifies the resource to be created or updated, and if the operation is successful there will be after a feature in that URL exactly as you sent, regardless of what was before.

For other cases, use POST.

For example, let’s assume an api that allows you to create or update users: POST /user returns a new user with the metadata sent in the request body. Normally the ID is created on the server and returned in the response. PUT /user/12345 creates or replaces user 12345 with the information sent in the body of the request

Note that in the first case if we repeat the same request theoretically we will be creating another user with the same data (but with different ids), or the second request will return some error, in the second case we will be replacing the user 12345 by another exactly the same if the data in the body of the request are the same.

Note: PUT is used when sending a complete object. If you want to update only a part of it and not resend everything, use PATCH.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.