1

Assuming I have an endpoint GET /api/foos/{id} which has optional parameters: includes, query, type should I create a link for each of the 'usecases' or can I include it as a single link?

Should it look more like this:

"_links":{
   "self": { "href": "/api/foos/1" },
   "includes": { "href": "/api/foos/1{?includes}", "templated": true },
   "query": { "href": "/api/foos/1{?query}", "templated": true },
   "type": { "href": "/api/foos/1{?type}", "templated": true },
}

Or maybe like this:

"_links":{
   "self": { "href": "/api/foos/1" },
   "query": { "href": "/api/foos/1{?includes}{?query}{?type}", "templated": true },
}

What if I also have paging related links, like next, prev etc. Should I include these templates for them, too? For example:

"next": { "href": "/api/foos?page=2{?includes}", "templated": true }
fiji3300
  • 103
  • 7
  • 1
    Your next link definitely should not be templated. If you are going to the next page in a collection, the next link should specifically refer to the next page for the current results. If that includes query parameters, these should probably be hardcoded with their actual values. – Evert Oct 13 '21 at 16:17
  • 2
    Also the correct syntax is `{?includes,query,type}` – Evert Oct 13 '21 at 16:17

1 Answers1

0

According to RFC6570, Section 3.2.1 (which is the foundation for URL templating) you can add multiple parameters and parameter without a value will be ignored:

A variable that is undefined (Section 2.3) has no value and is ignored by the expansion process.

That means for your example that you can use following HAL response:

"_links":{
   "self": { "href": "/api/foos/1" },
   "query": { "href": "/api/foos/1{?includes,query,type}", "templated": true },
}

And it should work for your paging example as well.

schrieveslaach
  • 1,689
  • 1
  • 15
  • 32