From what I understand of REST principles, URLs should represent a single resource, like a user or a product. How do you deal with resources that are random, or generated dynamically?
Suppose I create a resource called api.example.com/integer that returns a random integer. Would I still use GET to retrieve the integer? What would POST, PUT, and DELETE mean in this context?
What about URLs that represent behaviors? Suppose I create a resource called api.example.com/add that returns a sum of two numbers. If I wish to use this resource, do I use GET or POST to submit the numbers to be added?
It is not required that all resources support all verbs. That is what the OPTIONS verb is for to find out what verbs are supported.
I would say either of the following are pretty self explanatory
Either could be valid. Just be careful that a GET request may get cached. If it does then you would not be getting a random result.
One of the main principles behind REST is that you model your urls are representing nouns, not verbs. So http://api.example.com/add is not an ideal url.
You could do
or
with some standard format entity body that contains the numbers to add.
On the surface it may seem pretty pedantic differentiating between an url that ends with “Add” and one that ends with “summation”. However, this is a pretty simple example and the REST constraint is there to guide you towards a design that has certain desirable characteristics for distributed systems.
Many years ago people would argue the difference between
and
was not significant. I don’t think too many would dismiss the distinction these days.