Answer the question
In order to leave comments, you need to log in
How to correctly add an action to the rest api other than CRUD?
I don’t quite understand how it would be right from the point of view of architecture / design to add an action to the rest api using the example of a toolkit built into Yii2 for creating a restful service
. Example: there is a controller that has CRUD for creating parcels. Everything is clear with it: http get (parcel/index) - a list of parcels; http post (parcel/create) - creating a new one; put (parcel/123) - editing an existing one, etc.
But I also have a method that returns the cost of the package using the given parameters as an example without creating the package itself. How to be with him? What type of http request to send to it (I think post)? What url will it be correct to send a request to if the controller is called parcel?
Answer the question
In order to leave comments, you need to log in
Get a separate method, for example, calculate
. It is not standard, but it can be used and describes its purpose well.
Why not:
If you do as Nazar Mokrinsky suggests , then this is RPC, this is also a normal approach when resource names do not fit very well (for example, I do this with password recovery methods). It's better to use POST in this case, but then again in your case everything still fits the rest just fine, so....
I join the answer of Talgat Baltasov with a little addition. If the state of your system does not change as a result of the request, the GET method is unambiguous. You may need to add a new object (read controller), a calculator for calculating the cost of a parcel and ask him.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question