[英]RESTful GET with image
I have a REST API, and I am adding a new function that allows users to GET
some information from a resource based on a barcode image they provide.我有一个 REST API,我正在添加一个新的 function,它允许用户根据他们提供的条形码图像从资源中
GET
一些信息。
My question is: what is the correct way to do this in terms of best practices?我的问题是:就最佳实践而言,正确的方法是什么?
Below are some of my thoughts regarding the problem.以下是我对这个问题的一些想法。
GET:得到:
Using GET
, I would normally specify criteria in the URI like this: foo?name=bar
, but passing image data the same way will most likely fail due to the length ( looking at this ).使用
GET
,我通常会像这样在 URI 中指定条件: foo?name=bar
,但由于长度原因,以相同方式传递图像数据很可能会失败( 请看这个)。
According to these answers , passing data in the body instead of the URI does not seem like a good solution either.根据这些答案,在正文中传递数据而不是 URI 似乎也不是一个好的解决方案。
POST:邮政:
I could however use a POST
request, but this isn't very RESTful, as I am only retrieving information.不过,我可以使用
POST
请求,但这不是很 RESTful,因为我只是在检索信息。
Your resource will do two things你的资源会做两件事
You could model analyzing bar code image as a resource BarCodes .您可以 model 分析条码图像作为资源BarCodes 。 This resource accepts requests with images submitted via POST analyses the bar code image (1) and returns a URL in the location header that can be used to retrieve some data (2).
此资源接受通过POST提交的图像请求,分析条形码图像 (1) 并在位置header 返回一个 URL,可用于检索一些数据 (2)。
This approach splits the two functionalities your want to realize in two resources/steps.这种方法将您想要在两个资源/步骤中实现的两个功能分开。
You gain with this approach:您可以通过这种方法获得:
Firstly, the only way to pass image (file) data to a web server from a browser is via a POST request.首先,从浏览器将图像(文件)数据传递到 web 服务器的唯一方法是通过 POST 请求。 Be aware of this when you are looking for the best solution.
在寻找最佳解决方案时请注意这一点。
Second, the implication of the RESTful resource model is that resources have identifiers that are very short strings or numbers, and are assigned by the server on creation for later use by the client.其次,RESTful 资源 model 的含义是资源具有非常短的字符串或数字的标识符,并由服务器在创建时分配以供客户端以后使用。
Your problem doesn't fit neatly into the REST model. So you are clear to go ahead and use whatever method works in practice.您的问题不完全适合 REST model。因此您很清楚 go 并使用实际可行的任何方法。
On RESTful service resources are manipulated using a fixed set of operations:在 RESTful 服务上,资源是使用一组固定的操作来操纵的:
PUT : creates a new resource, which can be then deleted using DELETE
GET : retrieves the current state of a resource
POST: transfers a new state onto a resource
In your case your case you should look for GET operation.在您的情况下,您应该寻找GET操作。 The problem of requests having large amounts of input data cannot be encoded in a URI (i,e Error 414: URO too long) ) is only problem when you have to pass long parameter in URI, this is one limitaions of REST, for retriving this is not a problem, i have tried a REST service where the service return a banch of XML file.
具有大量输入数据的请求无法在 URI 中编码的问题(即错误 414:URO 太长))仅当您必须在 URI 中传递长参数时才会出现问题,这是 REST 的一个限制,用于检索这不是问题,我尝试了 REST 服务,该服务返回一组 XML 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.