0

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?

3
  • "can I do that?" Are you asking if it's physically possible or if doing so violates some design principle like rest? Commented May 23, 2020 at 13:32
  • If doing it I am violating some design principle in rest Commented May 23, 2020 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. Commented May 23, 2020 at 14:04

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.