GET请求的REST API URL模式设计[关闭]

问题描述 投票:-3回答:2

我目前正在为GET请求开发REST API。

我正在开发一个API,可以为应用程序,类别和平台返回赞助广告,例如:应用程序是谷歌,平台是移动站点,类别是主页横幅,以及基于上述输入返回广告的API。我可以为同一个应用程序提供多个类别,同一类别可以存在于任何应用程序或平台中。我不能确定如何配置URLpattern是正确的。

请告诉我一个好的URL模式。

我正在考虑以下选项。

  1. api / spons / {applicationId} / {category} /?platformId = 1
  2. api / spons / {category} / {applicationId} /?platformId = 1

其中category是一个字符串,例如壁纸,横幅等

c# rest api naming-conventions
2个回答
3
投票

根据您的描述,我对REST API的URI就是这样的

  • / api / application / {id} < - 返回(GET),创建(POST,PUT),更新(PUT)应用程序对象
  • / API /类别/ {ID}
  • / API /平台/ {ID}

每个对象都有唯一的URI。

如果我使用REST来访问和创建特定广告,它可能看起来像

  • / api / ad / {id} < - 返回(GET),创建(POST,PUT),更新(PUT)广告对象
  • / api / ad / < - 返回所有广告(GET)
  • / api / ad?application = {applicationId}&category = {categoryId}&platform = {platformId} < - 查询/过滤广告(GET)

可以使用RPC而不是REST,这有时更高效/更简单,特别是如果您想要返回“已连接”数据。例如:

  • / RPC / GetAds?应用= {的applicationID}&类别= {的categoryId}&平台= {platformId}
  • / rpc / GetAds?a = {applicationId}&c = {categoryId}&p = {platformId} < - 减少网络流量

一切都不一定是RESTful。


0
投票

我认为最好将值作为查询字符串传递,这是我们通常用搜索,过滤或排序操作做的事情。所以你不用担心订单

api/sponsored?category=1&applicationId=2&platformId=1
© www.soinside.com 2019 - 2024. All rights reserved.