Live data from Hacker News

Ask HN: Nested Resources in REST/HTTP API URLs?

news.ycombinator.com

41–50 of 72 posts

Re: Ask HN: Nested Resources in REST/HTTP API URLs?

#42
post #3

Just do: - /organizations/:id - /blogs/:id - /sections/:id - /threads/:id - /comments/:id Why? What determines how resources are related are links, not patterns in the URL. It's a graph, the URLs are just nodes, the links are what connect them. If you _want_ to have some sort of hierarchy in the URL, you can redirect: - /organizations/123/blogs/1234 -> /blogs/1234 Then each resource expresses how it is related to oth…

This is good for fetching a single item. What if you want to list resources? Eg. List all comments of a blog? It has to be GET /blog/:blogid/comments Then, how about creating a new comment? It'll be POST /blog/:blogid/comments When both of these have the blog's hierarchy in the URL, should we take it away when fetching a single item? Or the stay consistent, we should simply do: /blog/:blogid/comments/:commentid It's…

In this case, I think of this as more of a search endpoint than a list. What would you think about using a parameter to specify the relation? i.e.

GET /comments?blogId=

Re: Ask HN: Nested Resources in REST/HTTP API URLs?

#43
post #3

Just do: - /organizations/:id - /blogs/:id - /sections/:id - /threads/:id - /comments/:id Why? What determines how resources are related are links, not patterns in the URL. It's a graph, the URLs are just nodes, the links are what connect them. If you _want_ to have some sort of hierarchy in the URL, you can redirect: - /organizations/123/blogs/1234 -> /blogs/1234 Then each resource expresses how it is related to oth…

This seems like the most traditional and respectful of REST way to go. You could try a HATEOAS approach if you want to get a bit more fancy. https://restfulapi.net/hateoas/ If you are following REST strictly there should be only objects, actions and verbs.

> If you are following REST strictly there should be only objects, actions and verbs.

Not quite. That would be a resource-based architectures much like REST, but REST dials requirements a notch higher, with stuff like adding capabiliy-discovering semantics to resources such as navigation links to related resources and operations to express it's state and state-changed, in the form of HATEOAS.

Re: Ask HN: Nested Resources in REST/HTTP API URLs?

#44

I seriously think we need a serious update to REST api urls. There is no rule that says it has to be this hard. I would imagine something like graphql type of URL schema: api.xxx.com/{organizations: organizationId { blogs: blogId { sections: sectionId } } }

REST is not a strict specification and it's not a single implementation, you can just start doing it.

That said, I wouldn't recommend going the allow everything flexible resolver way like GraphQL: it's terrible for performance (eg. most APIs use N+1 queries unless you have something like https://github.com/join-monster/join-monster), the complexity of the codebase skyrockets and having to specify all the fields you want is not exactly ergonomic in most situations.

Re: Ask HN: Nested Resources in REST/HTTP API URLs?

#46
post #16
post #3

Just do: - /organizations/:id - /blogs/:id - /sections/:id - /threads/:id - /comments/:id Why? What determines how resources are related are links, not patterns in the URL. It's a graph, the URLs are just nodes, the links are what connect them. If you _want_ to have some sort of hierarchy in the URL, you can redirect: - /organizations/123/blogs/1234 -> /blogs/1234 Then each resource expresses how it is related to oth…

> - /organizations/:id > - /blogs/:id The pragmatic, large-company-only counterpoint is the narrow edge case where: - :id must be human-readable for "SEO reasons" - there are many competing organizations and blogs to the point where there may be a name collision. Although in that case, I'd still suggest: /:organization-name/:blog-name

What about /blogs/:id?organization=:id, doesn’t it solve the issue?

Re: Ask HN: Nested Resources in REST/HTTP API URLs?

#47
post #3

Just do: - /organizations/:id - /blogs/:id - /sections/:id - /threads/:id - /comments/:id Why? What determines how resources are related are links, not patterns in the URL. It's a graph, the URLs are just nodes, the links are what connect them. If you _want_ to have some sort of hierarchy in the URL, you can redirect: - /organizations/123/blogs/1234 -> /blogs/1234 Then each resource expresses how it is related to oth…

This would be my recommendation, especially if you use UUIDs or some other long string as identifiers

Re: Ask HN: Nested Resources in REST/HTTP API URLs?

#48
Go for logical structure and your query patterns.

If you plan querying blogs per organization, then make it a su resource under the organization.

If you are planning only to get a full list, go for root level blogs.

If you are planning to do both then do both :-)

https://dev.tasubo.com/2021/08/quick-practical-introduction-...

Re: Ask HN: Nested Resources in REST/HTTP API URLs?

#49
post #3

Just do: - /organizations/:id - /blogs/:id - /sections/:id - /threads/:id - /comments/:id Why? What determines how resources are related are links, not patterns in the URL. It's a graph, the URLs are just nodes, the links are what connect them. If you _want_ to have some sort of hierarchy in the URL, you can redirect: - /organizations/123/blogs/1234 -> /blogs/1234 Then each resource expresses how it is related to oth…

That's a poor advice.

APIs are consumed by humans first. Machines don't care about your structure.

HATEOS is the part that actually doesn't matter in practice.

Re: Ask HN: Nested Resources in REST/HTTP API URLs?

#50
The long form is more flexible because it gives you the full information about the state of the app. If you don't include the blogId in the URL, the front end will not know which blog the section or comment belongs to until it has finished loading from the server... So in the case of a single-page app, you can't show a 'Go back to blog' button in the UI until the page has finished loading... Though it's trivial to do if the blogId is in the URL.

A common pattern I follow with URLs is that I sometimes allow the user to omit the final ID in the URL; in this case I do a front-end redirect to the URL with the ID of the main/default resource appended at the end - This is useful if the user account has (for example) a 'main blog' associated with it.

Post reply on HN