Side Effects on Other Resources

This is a theory / best practice question regarding RESTful and HATEOAS design ...

Given the resources:

/ myresources / (collection of our resource objects)

and

/ transaction / (collection of historical transactions that occurred on the system)

This is a good practice for:

POST / myresources /

not only create a new resource in / myresources /, but also a new resource in / transaction /?

In other words, can POST (or any verb) use one resource for a URL for both this URL and others? Is there any other approach? Obviously, we could use two POSTs, but this requires us to trust the user to maintain the correct state when modifying several resources.

+6
source share
2 answers

Yes, that's great. Imagine another case where the system provides URIs /myresources/latest . When there are no resources, this can return 404, but when starting POSTing resources, both the canonical URI and the latest URI will return 200 OK. There are many benefits to this approach.

However, keep caching when developing such resources. For example, if you send POST to the /myresources/ collection, you will invalidate this collection in any cache. However, you will not invalidate the /transactions/ collection, and the two indexes may go out of sync. In any case, they may not be synchronized throughout the system, depending on the cache schedule between several clients and the server of origin, but often clients get the opportunity to expect this action to be synchronous from a distance, and caching can upset this in such cases.

+5
source

It seems quite reasonable to me. The person creating the new resource does not know if, for example, he could implement another client survey for new resources, then add the transaction resource, right?

Thus, at this level there is no conceptual problem, not to mention how "it is reasonable for the server level to create new resources."

+1
source

Source: https://habr.com/ru/post/912968/


All Articles