How to return random items RESTfully?

后端 未结 5 1519
有刺的猬
有刺的猬 2021-02-06 03:19

My design exposes two kinds of resources:

  1. Images
  2. Tags

I would like clients to be able to request random images by their tag(s). For example

5条回答
  •  走了就别回头了
    2021-02-06 03:46

    I've struggled myself with this issue. What we ended up implementing was an HttpResponseRedirect from, eg:

    http://www.example.com/randomNewYorkImage

    to a random New York image:

    http://www.example.com/images/New_York/1234.

    The first resource can be conceived as a random New York images dispatcher. This solution will load more the server, as it will be requested two resources, but it is as RESTful as you can get.

    Edited: Plus, if you are caching, each image will be in the cache, and your server goes from sending an image to sending only the redirect, as the cache will intercept the second request, and thus alleviating your server load.

提交回复
热议问题