1

I am doing attach and detach operations in my below controller like detaching the relationship from a OU to a ModuleDevice.

What http verbs would those operations mean in a REST API?

ModuleDevicesToOUController

Api/ModuleDevicesToOU/1/OU/1 // Attach for ModuleDevice with Id 1 the OU with Id 1
PUT/Delete/Patch ??? Attach…

Api/ModuleDevicesToOU/1/OU/1// Detach for ModuleDevice with Id 1 the OU with Id 1
PUT/Delete/Patch ??? Detach…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321

2 Answers2

0

How about using the LINK method? https://datatracker.ietf.org/doc/html/draft-snell-link-method-08

If that's a little experimental for you then just use POST.

Community
  • 1
  • 1
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • I have realized that I misunderstood the requirements. Now seeing that the SAVE method of the client is a POST AND this POST includes removing/adding child references of entities all in ONE http action. But I will have to use LINK in future too for real rest api ;-) – Elisabeth Feb 08 '14 at 16:31
0

If I understand your API correctly, the call is doing a change in the resource at the server, this means that you should use a POST:

Per RFC 2616 , the POST method should be used for any context in which a request is non-idempotent: that is, it causes a change in server state each time it is performed, such as submitting a comment to a blog post or voting in an online poll. In practice, GET is often reserved, not simply for idempotent actions, but for nullipotent ones, ones with no side-effects (in contrast to "no side effects on second or future requests" as with idempotent operations)

http://en.wikipedia.org/wiki/POST_%28HTTP%29

http://en.wikipedia.org/wiki/Http

This is, in my opinion, a very important point between the difference between GET and POST. POST is not only there to allow you to send information in the body, but also to modify the state of the server. I always have it in mind when designing REST interfaces.

I hope it helps.

Rafa
  • 2,328
  • 3
  • 27
  • 44
  • Why Post? "PUT is idempotent, so if you PUT an object twice, it has no effect." When I attach/detach twice is has no effect. Because it has already been attached/detached. What do you think? – Elisabeth Feb 05 '14 at 12:35
  • AND if use POST for both actions I can not put them in the same controller because they are double now that would not make sense and would not compile! – Elisabeth Feb 05 '14 at 12:38
  • @Elisa: That's is why I said that I may be understanding your API wrong. I'm not very sure about how your attach and detach works. If you don't expect a response, you can use PUT as well, you're right, but read carefully the limitations (http://tools.ietf.org/html/rfc2616#section-9.6) – Rafa Feb 05 '14 at 12:53
  • @Elisa: You cannot probably get your own error messages back and you will have to use the HTTP error codes to know if everything was fine. When implementing a REST interface I rarely use a PUT because it makes it harder to implement your own communication protocol based on DTOs. – Rafa Feb 05 '14 at 12:54