Where to store resource URIs in the REST API

I am working on a REST API and I cannot find a better way to manage resource URIs. When I request my API, I want something like this:

{ 'name':'foo', 'id':100, 'uri':'/path/to/the/resource/100' } 

Should I store resource URIs in my database or should I generate code while processing the request?

thanks

+4
source share
3 answers

Do not store URIs in your database.

The resource URI is not the resource itself. In addition, you do not need to return an identifier in your response. Customers do not need this. Just return the url for self and the urls of any related resources.

You can also rate this answer .

+1
source

In the design of the API, you should not return the URI as part of the data in the HTTP API response. There is a header field here , if your resource is accessible from several URIs, and the domain logic of your application, along with a good name for the URI, should allow you to guess the location of the resource.

For example, if {'name':'foo', 'id':100} is Foo , you (and users) usually expect its URI to be /foos/100 , so why do you need to return the URI inside the response data?

Now it’s more pragmatic, since support for these trendy HTTP headers can make it difficult for your API to accept the API, I believe that you should store URIs in a 1: lot relationship with your resources, if you expect your resources to change URIs often (I hope they don’t do), so it might be easier for you to support call forwarding with 301 Moved Permanently for older ones. In addition, I see no reason not to generate them on the fly, since you will have to redirect to old controller actions that respond to old URIs manually if you use the MVC structure.

0
source

From what I understand, you do not store the Uri, but you specify the uri pattern on the service methods using the URI pattern property. For example, using your link, you would:

 [WebGet(UriTemplate = /path/to/the/resource/{number})] public SomeMethod(String number) { Int16.Parse(number); //User user = UserList(number) } 
0
source

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


All Articles