Rest/Json -PUT vs POST

0

Pomożecie rozsądzić mały spór? Według mnie powinno być:

Zakładanie entity

  • POST serwer/kolekcja - serwer zakłada nowe entity, zwraca jego id
    Lub:
  • PUT serwer/kolekcja/nowy_id - serwer zakłada nowe entity o podanym id

Uaktualnienie entity (całego):

  • PUT serwer/kolekcja/id gdzie entity o wskazanym id musi istnieć.

Lista wszystkich:

  • GET serwer/kolekcja no ale to chyba jasne.

Za to, niektórzy upierają się żeby aktualizować przez POST zamiast PUT.

W końcu, jak jest prawidłowo?

0

Nijak. REST to tylko konwencja. Nie ma "prawidłowo", bo nie ma sztywnych zasad.

Przyjęło się, że PUTem wrzuca się nowy kontent, który można dostać GETem pod tym samym adresem, do którego poszedł PUT. Natomiast POSTem można modyfikować / wrzucać cokolwiek. To co opisałeś, wygląda sensownie.

Jednak to jest tylko konwencja i z punktu widzenia standardu protokołu HTTP możesz sobie aktualizować nawet GETem :P

0

No dobra, ale jakieś konwencje obowiązują. Czytałem dyskusję na StackOverflow i raczej przeważają opinie takie jak moja

0

Zapewne o to chodzi? http://stackoverflow.com/questions/630453/put-vs-post-in-rest

No w komentarzu najwyżej ocenionym:

POST:

Used to modify and update a resource

POST /questions/<existing_question> HTTP/1.1
Host: wahteverblahblah.com

Note that the following is an error:

POST /questions/<new_question> HTTP/1.1
Host: wahteverblahblah.com

If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a 'resource not found' error because <new_question> does not exist yet. You should PUT the <new_question> resource on the server first.

You could though do something like this to create a resources using POST:

POST /questions HTTP/1.1
Host: wahteverblahblah.com

Note that in this case the resource name is not specified, the new objects URL path would be returned to you.

PUT:

Used to create a resource, or overwrite it. While you specify the resources new URL.

For a new resource:

PUT /questions/<new_question> HTTP/1.1
Host: wahteverblahblah.com

To overwrite an existing resource:

PUT /questions/<existing_question> HTTP/1.1
Host: wahteverblahblah.com

Chociaż, tu znowu wątpliwości bo update resource przez POST? A PATCH? Chyba każdy robi jak chce.

1 użytkowników online, w tym zalogowanych: 0, gości: 1