综合技术

REST Name of the resource itinerary for get and ResourceByID and ResourceByName

I am trying to write 2 Rest GET methods.

  1. Get user by Id
  2. Get user by userName .

I need to know if there is any resource naming convention for this. Both my id and username are strings.

I came up with:

/api/{v}/users/{userid}
/api/{v}/users/username/{username}

However, 2) doesn’t seem correct and if I change 2) to /api/{v}/users/{username} , I am mapping to 1) as both id and username are strings. Or is it considered acceptable to use /api/{v}/userbyName/{username} ?

How should I name my resource route in case 2) ?

First of all: https://vimeo.com/17785736 (15 minutes which will solve all your questions)

And what is unique ? Is the username unique or only the id or both are unique?

There is a simple rule for all that:

/collection/item
However, 2) doesn't seem correct and if I change 2) to /api/{v}/users/{username}, I am mapping to 1) as both id and username are strings.

If your item can be identified with an id and also with an unique username – it doesn’t matter if it’s the username or the id – simply look for both (of course your backend needs to handle that) and retrieve it.

According to your needs this would be correct:

/api/{v}/users/{userid}

/api/{v}/users/{username}

but I would choose only to use: /api/{v}/users/{userid} and filter by username only with a query parameter (description for that down there below)

Also will I break any rules if I come up with /api/{v}/userbyName/{username}

Yes- /api/{v}/userbyName/{username} will break the rule about /collection/item because userByName is clearly not a collection it would be a function – but with a real restful thinking api there is no function in the path.

Another way to get the user by name would be using a filter/query paramter – so the ID will be available for the PathParameter and the username only as filter. which than would look like this:

/api/{v}/users/?username={username}

This also don’t break any rules – because the query parameter simply filters the whole collection and retrieves only the one where username = username.

How should I name my resource route in case 2) ?

Your 2) will break a rule – so I can’t/won’t suggest you a way to do it like this.

Have a look at this: https://vimeo.com/17785736 this simple presentation will help you a lot about understanding rest.

Why would you go this way?

Ever had a look at a javascript framework like – let’s say ember . ( Ember Rest-Adapter ). If you follow the idea described up there and maybe also have a look at the json format used by ember and their rest adapter – you can make your frontend developer speed up their process and save a lot of money + time.

展开阅读全文

微信扫一扫,分享到朋友圈

REST Name of the resource itinerary for get and ResourceByID and ResourceByName
0

Python’s Future is even above the ‘c’ level!

上一篇

Spatial enhancement in RavenDB

下一篇

你也可能喜欢

评论已经被关闭。

插入图片
REST Name of the resource itinerary for get and ResourceByID and ResourceByName

长按储存图像,分享给朋友