简体   繁体   English

如何设计REST API以允许返回带有元数据的文件

[英]How to design a REST API to allow returning files with metadata

Suppose I'm designing a REST API and I need the clients to be able to obtain files with metadata. 假设我正在设计一个REST API,我需要客户端能够获取带元数据的文件。 What is a good way to design the resources / operations? 设计资源/运营的好方法是什么?

Some ideas come to mind: 我想到了一些想法:

  • A single resource (ie GET /files/{fileId}), which returns a multi-part response containing both the file and a JSON/XML structure with metadata. 单个资源(即GET / files / {fileId}),它返回包含文件和带元数据的JSON / XML结构的多部分响应。 I have a feeling that this is not a very good approach. 我觉得这不是一个很好的方法。 For example, you cannot use the Accept header for the clients to determine if they want a XML or a JSON metadata representation, since the response type would be multi-part in both cases. 例如,您不能使用客户端的Accept标头来确定他们是否需要XML或JSON元数据表示,因为响应类型在两种情况下都是多部分的。

  • Two resources (ie GET /files/{fileId} and GET /files/{fileId}/metadata), where the first one returns the file itself and the second one a JSON/XML structure with metadata. 两个资源(即GET / files / {fileId}和GET / files / {fileId} / metadata),其中第一个返回文件本身,第二个是带有元数据的JSON / XML结构。 There can be a link from the metadata to the file. 可以存在从元数据到文件的链接。 However, how do I send a link to the metadata along with the file? 但是,如何将文件链接与文件一起发送?

I would suggest using the second idea you presented. 我建议使用你提出的第二个想法。 This is the strategy used by most of the major web drives (Box, Dropbox, Google Drive, etc). 这是大多数主要网络驱动器(Box,Dropbox,Google Drive等)使用的策略。 They often have a significantly different URL because they store content and metadata in disparate locations. 它们通常具有明显不同的URL,因为它们将内容和元数据存储在不同的位置。

You can add a Link header to the file response with a link to the metadata. 您可以使用指向元数据的链接向文件响应添加链接标头。 Link headers are described in RFC 5988 . RFC 5988中描述了链接头。 The set of currently-registered link relations is here . 当前注册的链接关系集合在这里 Off the cuff, it seems that the describedBy relation is appropriate here. 关闭袖口,似乎所describedBy关系在这里是合适的。

I've had success with the following kind of API design. 我在以下API设计方面取得了成功。 This differs slightly from what you suggested in that the main resource just contains links to its components. 这与您建议的略有不同,主要资源只包含指向其组件的链接。

POST /file
Request
  <bytes of file>
Response
  Location: /file/17
  {
    "id": 17
  }

GET /file/17
  {
    "data": "/file/data/17",
    "metadata": "/file/metadata/17"
  }

GET /file/data/17
  <bytes of file>

GET /file/metadata/17
  {
    "type": "image",
    "format": "png"
  }

DELETE /file/17

Your first Option is not at all a good choice because it violates following REST constraint. 您的第一个选项根本不是一个好的选择,因为它违反了以下REST约束。

Manipulation of resources through these representations under Uniform interface Principle. 通过 统一接口原则下的这些表示来操纵资源

When a client holds a representation of a resource, including any metadata attached, it has enough information to modify or delete the resource. 当客户端持有资源的表示(包括附加的任何元数据)时 ,它具有足够的信息来修改或删除资源。

If you brake it. 如果你刹车的话。 Your URL will not be consider as RESTful. 您的网址不会被视为RESTful。

Wiki about it. 关于它的Wiki

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM