How should I deal with object hierarchies in a RESTful API?

后端 未结 4 842
栀梦
栀梦 2020-12-22 19:57

I am currently designing the API for an existing PHP application, and to this end am investigating REST as a sensible architectural approach.

I believe I have a reas

4条回答
  •  既然无缘
    2020-12-22 20:30

    There's no reason not to combine these.

    • api.example.com/user/{user_id} – return a user representation
    • api.example.com/channel/{channel_id} – return a channel representation
    • api.example.com/user/{user_id}/channels – return a list of channel representations
    • api.example.com/user/{user_id}/channel_list – return a list of channel ids (or links to their full representations, using the above links)

    When in doubt, think about how you would display the data to a human user without "API" concerns: a user wants both index pages ({user_id}/channel_list) and full views ({user_id}/channels).

    Once you have that, just support JSON instead of (or in addition to) HTML as the representation format, and you have REST.

提交回复
热议问题