I have a collection /users/{userId}/tools

and I want to GET and POST to that collection. Can I have different representation of that object based on the method?

For example, for POST I want to send only

{"name": "Toolname", "material": "MaterialName"}

so I built a DTO with only that 2 fields.

And forGET I want to retrieve only the name

[{"name": "Toolname"}, {"name": "AnotherToolName"}, ...]

so I built a DTO with only the name.

Because it's the same URI, can I do that?

  • "can I do that?" Are you asking if it's physically possible or if doing so violates some design principle like rest? – candied_orange May 23 at 13:32
  • If doing it I am violating some design principle in rest – TheEmpire12 May 23 at 13:47
  • 1
    I don't think it matters. In all likelihood, you're not building a REST interface; this is simply a JSON web service. Read restfulapi.net and see if your service meets the criteria provided there. – Robert Harvey May 23 at 14:04

Because it's the same URI, can I do that?

Because the method is POST: yes, you can.

Consider the example of the world wide web; GET fetches a rich HTML document, with lots of interesting links, and images, and scripts. POST, on the other hand, just delivers a bunch of encoded key value pairs.

And it all works just fine.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.