Martin
Martin

Reputation: 40573

REST Architecture - How the Url of a complex method would look like?

I have the following Url which returns me the list of resources:

http://example.com/resources/

I also implemented a method which returns a specific resource (in this case, the resource 142).

http://example.com/resources/142

I would like to add a method which is outside the typical HTTP method: List, Create, Retrieve, Replace, Update. What is the pattern to follow? In my specific case, I need to check the availability of resource. How would the Url look like (http://example.com/resources/checkavailability/142)?

I though about simply using the GET method and retrieve that information as part of the object returned. However, some of my colleagues argue that this would not be efficient (the data to transfer would be much bigger than just returning true/false).

Thanks for the help!

Upvotes: 0

Views: 394

Answers (2)

manuel aldana
manuel aldana

Reputation: 16438

Restful over HTTP gives you uniform interface, you often don't need to encode the actions inside your URL

Regarding your mentioned /checkavailability using GET returning payload inefficiency is a valid reason, so use HEAD (it only gives you back the response headers).

request:
HEAD /resources/123

response status:
404 Not Found: equals to /checkavailability == false
200 OK: equals to /checkavailability == true

Other suggestions uniform interface replacements:

  • /resources/list : GET /resources
  • /resources/replace/123: PUT /resources/123
  • /resources/update/123: PUT /resources/123
  • /resources/create: POST /resources

Upvotes: 1

Anders
Anders

Reputation: 6218

There is no need for a resource to check the availability of another resource, and there is no need for a GET request, a HEAD request should be enough, this is the same as a GET request but without transferring the body. You can then look at the return codes, and via those determine if the resource is available. This is assuming you have properly implemented return codes.

Upvotes: 3

Related Questions