Home > database >  which is the better convention for REST API URL
which is the better convention for REST API URL

Time:08-19

I am new to REST API, and I am designing my own REST API for my web project.

Which of the followings is the better convention?

I want to access to the article that user ID 3 wrote.

/user/3/article
/article?user_id=3

Thanks.

CodePudding user response:

Which of the followings is the better convention?

They are both fine.

/article?user_id=3 may prove more convenient if you are expecting to use HTML forms as a way of finding resources.

/user/3/article may prove more convenient if you expecting to use dot segments to describe other resource identifiers in the hierarchy.


What if I want to access to the fourth article that User ID3 wrote? /user/3/article/4 is appropriate? I think this hierarchy is unnecessarily deep.

Deep hierarchies are fine. Not using deep hierarchies are also fine.

In some designs, we'll use resource identifiers for items that are not part of the same hierarchy as the collection itself

context="/user/3/article" rel="item" href="/articles/4"
context="/user/3/article" rel="item" href="/articles/9"
context="/user/3/article" rel="item" href="/articles/16"

Think "web page with links"; if you can follow a link, you don't need a formula to compute the URI yourself.

  • Related