How does POST differ from PUT when dealing with existing URIS?

Asked

Viewed 35 times

-2

Not only this site, but several others use the example of creating a user on a website:

www.site.com.br/user/id_do_usuario

To clarify that PUT would only update the old user with the entered data, and POST would create a new one, when requesting an existing id. But how POST deals with creating a new user, if the URIS are the same?

  • 1

    And what would be the problem of being the same? In fact, there are numerous Apis that have a single URL for various operations. Who defines how she will behave is who wrote the API. Perhaps it would be interesting if you [Dit] the post and better base where you got the conclusion that the URI would determine the behavior of the API, or restrict its functionality in any way. Another example: I could have an API with the path http://nomedosite/x and do all operations with POST (delete user, create, edit, delete property, etc.) just changing the data sent.

  • @Bacco for example, when creating a user profile based on the name defined by the client in an input field. I don’t understand how different users could have the same URI

  • 1

    Remember that URL is opaque and does not necessarily reflect the logic of the application. If I want to have in my API the URL /batatas and use it to send an email to the US President in the GET method I could.

  • 1

    abcson precisely, and what induced you to think that one thing has relation to the other (the URI with the result)?

1 answer

1

An HTTP request is more than a URI, it includes a header and possibly a useful load (the body). Most of this request is filled automatically by the browser, but it in its total looks like this:

GET /docs/index.html HTTP/1.1
Host: www.nowhere123.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

As you can see, in addition to the URI, the method is also sent, and so the server can handle the response according to the received request.

Browser other questions tagged

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