简体   繁体   中英

Hypermedia API - Is direct requests an anti pattern?

I'm designing an API using Hypermedia concepts. I saw that is a good practice have a root route that returns the others API links to navigate.

Ex.: I made a request to http://myapi.com and it returns

{
  links: [
    { 'rel': 'profile', href: '/profile' },
    { 'rel': 'orders', href: '/orders' },
    { 'rel': 'order_types', href: '/order_types' },
    ...
  ]
}

But, in this way, if the root had 20 resources it will be very large.

So, made a direct request to order_types instead request root and then request the order_types is a anti pattern?

It's a bit of a hard question to answer, because the first thing that comes to mind to me is: is it actually that large?

Right now for example each of your links takes around 40 bytes. For 20 resources that's 800 bytes. gzip the response (or brotli if you can support it) and you can make this much smaller. If your total file has some overhead (say another 200 bytes) and we assume 1KB, with something like gzip you can probably drop this back to 400 bytes or smaller.

You can set cache headers on this file so it only gets requested once in a blue moon.

So now your main question? Is it an anti-pattern. I would say yes, you should only have one bookmarked url.

As I am currently suffering a lot under a large scale HATEOAS style REST API, from my point of view do yourself a favor and place all links (methods) on the start page.

You really do not want to follow several links to find the method you want to use (or test). Of course you could use the direct path, but then again its just RPC-style again.

And as I am sorry to say, the whole concept of HATEOAS though not sounding bad IMHO is a real pain when it comes to implementing and testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM