API URL设计 – 查询参数或路径参数

huangapple go评论90阅读模式
英文:

API URL Design - query or path parameters

问题

/objectA/fromObjectBId/:id

英文:

We have a model where Object A is one-to-many with Object B.

We have an API where we want to get object A by it's Id so we have the URL /objectA/:id. That's fine and seems pretty standard.
We also need an endpoint where we can get Object A from object B's Id. (The parent of object B)

What would be the best solution for the URL for this? Should we be using path parameters or query parameters?

/objectA?objectBId=:id

/objectA/fromObjectBId/:id

something else?

答案1

得分: 1

首先,我鼓励你将你的URI视为标识符(IDs),而不是让客户端尝试从IDs构建URI。

但是,既然你谈到了关系,我倾向于从已知关系的一方指示。因此,不要尝试以获取ObjectA的URL根为基础来建模一切,而是从ObjectB出发进行遍历。

因此,路径可能看起来像:

/ObjectB/1234/ObjectA

英文:

First, I'd encourage you to think of your URIs as the IDs and not have the client try to construct URIs from IDs.

That said, since your talking about relationship, I'd tend to indicate that from the side of the relationship that's known. So instead of trying to model everything in terms of 'getting' ObjectA by using that URL root, instead traverse to it from ObjectB.

So the path might look something like:

/ObjectB/1234/ObjectA

huangapple
  • 本文由 发表于 2023年8月10日 19:27:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定