Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a REST API without resource? [closed]

I want to build a lightweight web service. Clients should use this service to perform different task. I read a lot about the concept of REST and now I have a problem to identify a resource.

All examples I have found so far are easy to understand and have a clear resource like "product", "book" or something like that. In my case I don't want to fetch, create or delete something from a database. I want to simply perform an action.

E.g: A client should send an access token (instead of username and password) and a url to the API. If the token is valid the server(API) starts a download and will do an upload to a cloud service. How am I supposed to do this with a REST API? All sources explain that verbs shouldn't be used in a URI. Usually I would use a method name like "uploadMediaContentToCloud" or something like that. I have no representation of MediaContent in a database. The action is neither a fetch nor a create so I'm not sure to use GET or POST or ....?

For any advice or article I should read I'm thankful.

EDIT: I just want to make sure that the upload was just an example. The server should perform a task (download something, upload something, run a script etc.) and my question is how am i supposed to build/design the URI if I shouldn't use verbs in the URI.

like image 639
Lotus Avatar asked Oct 19 '25 06:10

Lotus


2 Answers

For a start assuming you are fine with any language, here you can find discussion about implementing a web service to upload file. Here is an actual script to implement upload function.

EDIT:

When you cannot visualize the provides service as a resource you can name them verbs(But most of the REST api provide resources). For example you can find twitter api with services like destroy and echonest api with services like upload etc,. Here is a best practice blog for uri design.

like image 164
gman Avatar answered Oct 21 '25 04:10

gman


Don't get tangled up in nouns and verbs. Figure out what info you need to send to accomplish the operation, then structure a URL to handle it.

Think of the URL like a function call: the path (before the ?) represents the function name, and the query (after the ?) represents the parameters.

In your case, you need to upload a file, so you'll want to use POST. In the URL, you're going to tell the server that you're sending it a file, and what to do with it. The body of the request will primarily be just the contents of the file itself, all the instructions about what to do with it are encoded in the URL.

like image 38
TedB Avatar answered Oct 21 '25 04:10

TedB