4

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.

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).

According to these answers, passing data in the body instead of the URI does not seem like a good solution either.

POST:

I could however use a POST request, but this isn't very RESTful, as I am only retrieving information.

Community
  • 1
  • 1
whirlwin
  • 16,044
  • 17
  • 67
  • 98

3 Answers3

4

Your resource will do two things

  1. Analyse the bar code image and get some id
  2. Provide information to the client retrieved from some (data) source using the id above

You could model analyzing bar code image as a resource 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).

This approach splits the two functionalities your want to realize in two resources/steps.

You gain with this approach:

  • REST conforming interface
  • You can decide to process the bar code image extraction task asynchronous and return just an URL in the location header to retrieve status about the bar code extraction process
  • Separate functionality of bar code analyzing and information retrieval
saintedlama
  • 6,838
  • 1
  • 28
  • 46
1

Firstly, the only way to pass image (file) data to a web server from a browser is via a POST request. 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.

Your problem doesn't fit neatly into the REST model. So you are clear to go ahead and use whatever method works in practice.

Michael Slade
  • 13,802
  • 2
  • 39
  • 44
0

On RESTful service resources are manipulated using a fixed set of operations:

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. 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.

LeTex
  • 1,452
  • 1
  • 14
  • 28