Handling Actions in REST Web Services

I have web applications in which the user can upload samples that can be processed by the backend. Each sample can have one or more files of different types. As soon as the information about the sample is downloaded, the user can request a sample for analysis immediately or later (in some cases, the analysis may take several hours). The backend is based on REST services.

How do I tell the server to start the analysis? One of the principles of REST is that URLs should be based on a noun.

Therefore i cannot use

/startAnalysis?sampleId=55&startTime=now 

How about canceling the analysis?

 /cancelAnalysis?sampleId=57 
+4
source share
2 answers

Theory:

In fact, you are allowed to have API calls that send a response that is not a resource, they are called Actions , and when working with REST Actions you should use verbs, not nouns.

For example, the REST API converts 100 Euros to Chinese Yen:

 `/convert?from=EUR&to=CNY&amount=100` 

Thus, the valid REST actions startAnalysis and cancelAnalysis .

I recommend that you read Web Design API (free e-book) from apigee , which is an excellent brief introduction to the design of the REST API. It also covers REST actions.

Practice:

You can also imagine that the โ€œstart of analysisโ€ is part of the state of your Analysis resource. Instead of using actions, you can use PATCH or PUT to update the status of your Analysis resource.

And a more complex solution might be:

  • Have an example resource: / sample
  • You have an Analysis: / analysis resource
  • After creating a Sample resource with POST or PUT, you can create a POST Analysis resource over the created Sample resource: / sample / 1234 / analysis
  • To start the analysis of the sample (you can even post information about when to start the analysis).
  • If you want to cancel the analysis of the selection, you can delete the DELETE previously created Analysis resource: / sample / 1234 / analysis / abcde
+5
source

I think the REST principle you mention is actually only used for GET uris. GET uris must be a noun because you are simply requesting a resource, possibly with parameters. for PUT or POST uri, they should be verbs instead, because they cause a state change.

0
source

Source: https://habr.com/ru/post/1481003/


All Articles