名称或ID为restl api的url

use*_*268 2 rest restful-url nested-routes

我目前用一条users路线建立一个宁静的api .如果用户名是唯一的,那么使用用户名或他的id作为参数更具有特异性吗?

example.com/api/users/johndoe
Run Code Online (Sandbox Code Playgroud)

要么

example.com/api/users/123456
Run Code Online (Sandbox Code Playgroud)

此外,我计划了project资源的路线.如果我在这里要求project-id,它会是一个不一致的设计吗?

pBu*_*uch 8

通常可以更容易地记住用户名,但我建议您使用用户名中的ID,可能有空格或其他字符(例如德语变音符号),这些字符很难进入网址的良好格式.也可能是用户更改了他的名字.然后,所有链接都与ID相反无效.

  • 如果需要,还可以创建搜索路径,例如`example.com/api/users/search?name = johndoe`. (2认同)