Retrieving a collection of IDs instead of the full representation of a resource in REST

后端 未结 2 895
南旧
南旧 2021-01-19 01:17

I\'m designing a REST API for the first time, so I have what I consider a quite basic question about its design.

I would like the files collection to return an ID (o

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 02:07

    Just do it.

    I would call it a standard RESTful API design pattern to have an abbreviated resource representation in your collections resource and the full representation only on your entity resource.

    So /files would return something like:

    [
      {
        name: "foo",
        url: "example.org/files/3321"
      },
      {
        name: "bar",
        url: "example.org/files/3192910"
      }
    ]
    

    While /files/3321 returns the full file representation

    {
      name: "foo",
      self: "example.org/files/3321"
      encoding: "UTF-8",
      type: "xml-document"
    }
    

提交回复
热议问题