r/restAPI • u/[deleted] • Mar 07 '22
Separate DTO for GET and POST
Should we create separate DTO For GET and POST requests. I am really confuse regarding this. What are the best practices?
Any project to read code to learn more about REST api ?
3
Upvotes
1
u/mumfordco 24d ago
If you are asking about request models, then yes I would recommend you split them.
A POST requires strict validation while a GET usually takes optional filters and query params. Trying to use one model to handle both means you'll most likely end up with nullable validation fields on the GET request model.
1
u/Ankhara333 Sep 18 '24
No. The model for an object like "customer" should be the same if you are reading or writing it.
For requests, I recommend you use the same model as the response (GET's included although this is not always supported). If you have complex search criteria or dynamic metadata that is non-sensitive, it can go in the query params.