├── README.md ├── draft-kelly-json-hal.txt └── draft-kelly-json-hal.xml /README.md: -------------------------------------------------------------------------------- 1 | The hal+json specification 2 | ========================== 3 | 4 | The easiest place to read it is here: http://tools.ietf.org/html/draft-kelly-json-hal 5 | 6 | Please contribute proposals for changes that you think will improve the spec! 7 | 8 | 9 | How to propose a change 10 | ======================= 11 | 12 | Simple: 13 | 14 | Edit either the txt or the xml file, whichever one you want - and submit as a pull request. 15 | 16 | Wonk-mode: 17 | 18 | The txt is actually generated from the xml so it's better if you edit the latter. 19 | You also get extra brownie points if you: 20 | * install xml2rfc (`pip install xml2rfc`) 21 | * generate the txt from the xml (`xml2rfc xml2rfc draft-kelly-json-hal.xml --text`) 22 | * submit both in your PR 23 | -------------------------------------------------------------------------------- /draft-kelly-json-hal.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Network Working Group M. Kelly 6 | Internet-Draft Stateless 7 | Intended status: Informational 19 October 2023 8 | Expires: 21 April 2024 9 | 10 | 11 | JSON Hypertext Application Language 12 | draft-kelly-json-hal-11 13 | 14 | Abstract 15 | 16 | This document proposes a media type for representing resources and 17 | their relations with hyperlinks. 18 | 19 | Status of This Memo 20 | 21 | This Internet-Draft is submitted in full conformance with the 22 | provisions of BCP 78 and BCP 79. 23 | 24 | Internet-Drafts are working documents of the Internet Engineering 25 | Task Force (IETF). Note that other groups may also distribute 26 | working documents as Internet-Drafts. The list of current Internet- 27 | Drafts is at https://datatracker.ietf.org/drafts/current/. 28 | 29 | Internet-Drafts are draft documents valid for a maximum of six months 30 | and may be updated, replaced, or obsoleted by other documents at any 31 | time. It is inappropriate to use Internet-Drafts as reference 32 | material or to cite them other than as "work in progress." 33 | 34 | This Internet-Draft will expire on 21 April 2024. 35 | 36 | Copyright Notice 37 | 38 | Copyright (c) 2023 IETF Trust and the persons identified as the 39 | document authors. All rights reserved. 40 | 41 | This document is subject to BCP 78 and the IETF Trust's Legal 42 | Provisions Relating to IETF Documents (https://trustee.ietf.org/ 43 | license-info) in effect on the date of publication of this document. 44 | Please review these documents carefully, as they describe your rights 45 | and restrictions with respect to this document. Code Components 46 | extracted from this document must include Revised BSD License text as 47 | described in Section 4.e of the Trust Legal Provisions and are 48 | provided without warranty as described in the Revised BSD License. 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | Kelly Expires 21 April 2024 [Page 1] 57 | 58 | Internet-Draft JSON Hypertext Application Language October 2023 59 | 60 | 61 | Table of Contents 62 | 63 | 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 2 64 | 2. Requirements . . . . . . . . . . . . . . . . . . . . . . . . 3 65 | 3. HAL Documents . . . . . . . . . . . . . . . . . . . . . . . . 3 66 | 4. Resource Objects . . . . . . . . . . . . . . . . . . . . . . 4 67 | 4.1. Reserved Properties . . . . . . . . . . . . . . . . . . . 4 68 | 4.1.1. _links . . . . . . . . . . . . . . . . . . . . . . . 4 69 | 4.1.2. _embedded . . . . . . . . . . . . . . . . . . . . . . 4 70 | 5. Link Objects . . . . . . . . . . . . . . . . . . . . . . . . 5 71 | 5.1. href . . . . . . . . . . . . . . . . . . . . . . . . . . 5 72 | 5.2. templated . . . . . . . . . . . . . . . . . . . . . . . . 5 73 | 5.3. type . . . . . . . . . . . . . . . . . . . . . . . . . . 5 74 | 5.4. deprecation . . . . . . . . . . . . . . . . . . . . . . . 5 75 | 5.5. name . . . . . . . . . . . . . . . . . . . . . . . . . . 6 76 | 5.6. profile . . . . . . . . . . . . . . . . . . . . . . . . . 6 77 | 5.7. title . . . . . . . . . . . . . . . . . . . . . . . . . . 6 78 | 5.8. hreflang . . . . . . . . . . . . . . . . . . . . . . . . 6 79 | 6. Example Document . . . . . . . . . . . . . . . . . . . . . . 6 80 | 7. Media Type Parameters . . . . . . . . . . . . . . . . . . . . 8 81 | 7.1. profile . . . . . . . . . . . . . . . . . . . . . . . . . 8 82 | 8. Recommendations . . . . . . . . . . . . . . . . . . . . . . . 8 83 | 8.1. Self Link . . . . . . . . . . . . . . . . . . . . . . . . 8 84 | 8.2. Link relations . . . . . . . . . . . . . . . . . . . . . 8 85 | 8.3. HAL curies . . . . . . . . . . . . . . . . . . . . . . . 8 86 | 8.4. Hypertext Cache Pattern . . . . . . . . . . . . . . . . . 9 87 | 9. Security Considerations . . . . . . . . . . . . . . . . . . . 10 88 | 10. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 11 89 | 11. Normative References . . . . . . . . . . . . . . . . . . . . 11 90 | Appendix A. Acknowledgements . . . . . . . . . . . . . . . . . . 11 91 | Appendix B. Frequently Asked Questions . . . . . . . . . . . . . 12 92 | B.1. How should a client know the meaning/structure/semantics/ 93 | type of a resource? . . . . . . . . . . . . . . . . . . . 12 94 | B.2. Where can I find libraries for working with HAL? . . . . 12 95 | B.3. Why are the reserved properties prefixed with an 96 | underscore? . . . . . . . . . . . . . . . . . . . . . . . 12 97 | B.4. Are all underscore-prefixed properties reserved? . . . . 12 98 | B.5. Why does HAL have no forms? . . . . . . . . . . . . . . . 12 99 | Author's Address . . . . . . . . . . . . . . . . . . . . . . . . 12 100 | 101 | 1. Introduction 102 | 103 | There is an emergence of non-HTML HTTP applications ("Web APIs") 104 | which use hyperlinks to direct clients around their resources. 105 | 106 | The JSON Hypertext Application Language (HAL) is a standard which 107 | establishes conventions for expressing hypermedia controls, such as 108 | links, with JSON [RFC4627]. 109 | 110 | 111 | 112 | Kelly Expires 21 April 2024 [Page 2] 113 | 114 | Internet-Draft JSON Hypertext Application Language October 2023 115 | 116 | 117 | HAL is a generic media type with which Web APIs can be developed and 118 | exposed as series of links. Clients of these APIs can select links 119 | by their link relation type and traverse them in order to progress 120 | through the application. 121 | 122 | HAL's conventions result in a uniform interface for serving and 123 | consuming hypermedia, enabling the creation of general-purpose 124 | libraries that can be re-used on any API utilising HAL. 125 | 126 | The primary design goals of HAL are generality and simplicity. HAL 127 | can be applied to many different domains, and imposes the minimal 128 | amount of structure necessary to cover the key requirements of a 129 | hypermedia Web API. 130 | 131 | 2. Requirements 132 | 133 | The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 134 | "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 135 | document are to be interpreted as described in [RFC2119]. 136 | 137 | 3. HAL Documents 138 | 139 | A HAL Document uses the format described in [RFC4627] and has the 140 | media type "application/hal+json". 141 | 142 | Its root object MUST be a Resource Object. 143 | 144 | For example: 145 | 146 | GET /orders/523 HTTP/1.1 147 | Host: example.org 148 | Accept: application/hal+json 149 | 150 | HTTP/1.1 200 OK 151 | Content-Type: application/hal+json 152 | 153 | { 154 | "_links": { 155 | "self": { "href": "/orders/523" }, 156 | "warehouse": { "href": "/warehouse/56" }, 157 | "invoice": { "href": "/invoices/873" } 158 | }, 159 | "currency": "USD", 160 | "status": "shipped", 161 | "total": 10.20 162 | } 163 | 164 | 165 | 166 | 167 | 168 | Kelly Expires 21 April 2024 [Page 3] 169 | 170 | Internet-Draft JSON Hypertext Application Language October 2023 171 | 172 | 173 | Here, we have a HAL document representing an order resource with the 174 | URI "/orders/523". It has "warehouse" and "invoice" links, and its 175 | own state in the form of "currency", "status", and "total" 176 | properties. 177 | 178 | 4. Resource Objects 179 | 180 | A Resource Object represents a resource. 181 | 182 | It has two reserved properties: 183 | 184 | (1) "_links": contains links to other resources. 185 | 186 | (2) "_embedded": contains embedded resources. 187 | 188 | All other properties MUST be valid JSON, and represent the current 189 | state of the resource. 190 | 191 | 4.1. Reserved Properties 192 | 193 | 4.1.1. _links 194 | 195 | The reserved "_links" property is OPTIONAL. 196 | 197 | It is an object whose property names are link relation types (as 198 | defined by [RFC5988]) and values are either a Link Object or an array 199 | of Link Objects. The subject resource of these links is the Resource 200 | Object of which the containing "_links" object is a property. 201 | 202 | For a given link relation, servers SHOULD NOT change the property 203 | between a Link Object and array of Link Objects. Such changes SHOULD 204 | introduce a new link relation. This is to avoid breaking changes for 205 | existing clients coupled to the original choice, and is a 206 | consideration for any JSON based API. 207 | 208 | 4.1.2. _embedded 209 | 210 | The reserved "_embedded" property is OPTIONAL 211 | 212 | It is an object whose property names are link relation types (as 213 | defined by [RFC5988]) and values are either a Resource Object or an 214 | array of Resource Objects. 215 | 216 | Embedded Resources MAY be a full, partial, or inconsistent version of 217 | the representation served from the target URI. 218 | 219 | 220 | 221 | 222 | 223 | 224 | Kelly Expires 21 April 2024 [Page 4] 225 | 226 | Internet-Draft JSON Hypertext Application Language October 2023 227 | 228 | 229 | 5. Link Objects 230 | 231 | A Link Object represents a hyperlink from the containing resource to 232 | a URI. It has the following properties: 233 | 234 | 5.1. href 235 | 236 | The "href" property is REQUIRED. 237 | 238 | Its value is either a URI [RFC3986] or a URI Template [RFC6570]. 239 | 240 | If the value is a URI Template then the Link Object SHOULD have a 241 | "templated" attribute whose value is true. 242 | 243 | 5.2. templated 244 | 245 | The "templated" property is OPTIONAL. 246 | 247 | Its value is boolean and SHOULD be true when the Link Object's "href" 248 | property is a URI Template. 249 | 250 | Its value SHOULD be considered false if it is undefined or any other 251 | value than true. 252 | 253 | 5.3. type 254 | 255 | The "type" property is OPTIONAL. 256 | 257 | Its value is a string used as a hint to indicate the media type 258 | expected when dereferencing the target resource. 259 | 260 | 5.4. deprecation 261 | 262 | The "deprecation" property is OPTIONAL. 263 | 264 | Its presence indicates that the link is to be deprecated (i.e. 265 | removed) at a future date. Its value is a URL that SHOULD provide 266 | further information about the deprecation. 267 | 268 | A client SHOULD provide some notification (for example, by logging a 269 | warning message) whenever it traverses over a link that has this 270 | property. The notification SHOULD include the deprecation property's 271 | value so that a client maintainer can easily find information about 272 | the deprecation. 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | Kelly Expires 21 April 2024 [Page 5] 281 | 282 | Internet-Draft JSON Hypertext Application Language October 2023 283 | 284 | 285 | 5.5. name 286 | 287 | The "name" property is OPTIONAL. 288 | 289 | Its value MAY be used as a secondary key for selecting Link Objects 290 | which share the same relation type. 291 | 292 | 5.6. profile 293 | 294 | The "profile" property is OPTIONAL. 295 | 296 | Its value is a string which is a URI that hints about the profile (as 297 | defined by [RFC6906]) of the target resource. 298 | 299 | 5.7. title 300 | 301 | The "title" property is OPTIONAL. 302 | 303 | Its value is a string and is intended for labelling the link with a 304 | human-readable identifier (as defined by [RFC5988]). 305 | 306 | 5.8. hreflang 307 | 308 | The "hreflang" property is OPTIONAL. 309 | 310 | Its value is a string and is intended for indicating the language of 311 | the target resource (as defined by [RFC5988]). 312 | 313 | 6. Example Document 314 | 315 | The following is an example document representing a list of orders 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | Kelly Expires 21 April 2024 [Page 6] 337 | 338 | Internet-Draft JSON Hypertext Application Language October 2023 339 | 340 | 341 | GET /orders HTTP/1.1 342 | Host: example.org 343 | Accept: application/hal+json 344 | 345 | HTTP/1.1 200 OK 346 | Content-Type: application/hal+json 347 | 348 | { 349 | "_links": { 350 | "self": { "href": "/orders" }, 351 | "next": { "href": "/orders?page=2" }, 352 | "find": { "href": "/orders{?id}", "templated": true } 353 | }, 354 | "_embedded": { 355 | "orders": [{ 356 | "_links": { 357 | "self": { "href": "/orders/123" }, 358 | "basket": { "href": "/baskets/98712" }, 359 | "customer": { "href": "/customers/7809" } 360 | }, 361 | "total": 30.00, 362 | "currency": "USD", 363 | "status": "shipped", 364 | },{ 365 | "_links": { 366 | "self": { "href": "/orders/124" }, 367 | "basket": { "href": "/baskets/97213" }, 368 | "customer": { "href": "/customers/12369" } 369 | }, 370 | "total": 20.00, 371 | "currency": "USD", 372 | "status": "processing" 373 | }] 374 | }, 375 | "currentlyProcessing": 14, 376 | "shippedToday": 20 377 | } 378 | 379 | Here, the order list document provides a "next" link directing to the 380 | next page, and a "find" link containing a URI Template which can be 381 | expanded with an 'id' variable to go directly to a specific order. 382 | 383 | It also has two embedded resources, "orders". Each of these has its 384 | own links to the associated "basket" and "customer" resources, and 385 | properties showing their "total", "currency" and "status". 386 | 387 | Additionally, the order list resource has its own properties 388 | "currentlyProcessing" and "shippedToday". 389 | 390 | 391 | 392 | Kelly Expires 21 April 2024 [Page 7] 393 | 394 | Internet-Draft JSON Hypertext Application Language October 2023 395 | 396 | 397 | 7. Media Type Parameters 398 | 399 | 7.1. profile 400 | 401 | The media type identifier application/hal+json MAY also include an 402 | additional "profile" parameter (as defined by [RFC6906]) 403 | 404 | HAL documents that are served with the "profile" parameter still 405 | SHOULD include a "profile" link belonging to the root resource. 406 | 407 | 8. Recommendations 408 | 409 | 8.1. Self Link 410 | 411 | Each Resource Object SHOULD contain a 'self' link that corresponds 412 | with the IANA registered 'self' relation (as defined by [RFC5988]) 413 | whose target is the resource's URI. 414 | 415 | 8.2. Link relations 416 | 417 | Custom link relation types (Extension Relation Types in [RFC5988]) 418 | SHOULD be URIs (or curies) that when dereferenced in a web browser 419 | provide relevant documentation, in the form of an HTML page, about 420 | the meaning and/or behaviour of the target Resource. This will 421 | improve the discoverability of the API. 422 | 423 | 8.3. HAL curies 424 | 425 | HAL etablishes a mechanism called "curies" which allows for link 426 | relation types that are compact and more human readable (eg. 427 | "acme:widgets"), whilst still offering a way that they MAY be 428 | expanded into a dereferencable URI providing documentation (eg. 429 | "https://docs.acme.com/relations/widgets") 430 | 431 | To this end, HAL documents have a reserved link relation type called 432 | "curies". 433 | 434 | HAL curies are established for a given Resource Object via an array 435 | of Link Objects with the "curies" reserved link relation type. These 436 | links contain a URI Template with the token 'rel', and are named via 437 | the "name" property. 438 | 439 | The following demonstrates the relation 440 | "https://docs.acme.com/relations/widgets" being abbreviated to 441 | "acme:widgets" using curies: 442 | 443 | 444 | 445 | 446 | 447 | 448 | Kelly Expires 21 April 2024 [Page 8] 449 | 450 | Internet-Draft JSON Hypertext Application Language October 2023 451 | 452 | 453 | { 454 | "_links": { 455 | "self": { "href": "/orders" }, 456 | "curies": [{ 457 | "name": "acme", 458 | "href": "https://docs.acme.com/relations/{rel}", 459 | "templated": true 460 | }], 461 | "acme:widgets": { "href": "/widgets" } 462 | } 463 | } 464 | 465 | HAL curies can be used to create versioned link relation types like 466 | so: 467 | 468 | { 469 | "_links": { 470 | "self": { "href": "/" }, 471 | "curies": [{ 472 | "name": "v1", 473 | "href": "https://docs.example.com/relations/v1/{rel}", 474 | "templated": true 475 | },{ 476 | "name": "v2", 477 | "href": "https://docs.example.com/relations/v2/{rel}", 478 | "templated": true 479 | }], 480 | "v1:orders": { 481 | "href": "https://api.example.com/orders", 482 | "deprecation": "https://dev.example.com/deprecations/v1-orders" 483 | }, 484 | "v2:orders": { "href": "https://api.example.com/order-list" } 485 | } 486 | } 487 | 488 | In cases where an embedded Resource defines its own curies which 489 | conflict with those of its parent then, for links within this 490 | resource, these are overwritten and SHOULD take precedence over the 491 | curies of the parent. 492 | 493 | 8.4. Hypertext Cache Pattern 494 | 495 | The "hypertext cache pattern" allows servers to use embedded 496 | resources to dynamically reduce the number of requests a client 497 | makes, improving the efficiency and performance of the application. 498 | 499 | 500 | 501 | 502 | 503 | 504 | Kelly Expires 21 April 2024 [Page 9] 505 | 506 | Internet-Draft JSON Hypertext Application Language October 2023 507 | 508 | 509 | Clients MAY be automated for this purpose so that, for any given link 510 | relation, they will read from an embedded resource (if present) in 511 | preference to traversing a link. 512 | 513 | To activate this client behaviour for a given link, servers SHOULD 514 | add an embedded resource into the representation with the same 515 | relation. 516 | 517 | Servers SHOULD NOT entirely "swap out" a link for an embedded 518 | resource (or vice versa) because client support for this technique is 519 | OPTIONAL. 520 | 521 | The following examples shows the hypertext cache pattern applied to 522 | an "author" link: 523 | 524 | Before: 525 | 526 | { 527 | "_links": { 528 | "self": { "href": "/books/the-way-of-zen" }, 529 | "author": { "href": "/people/alan-watts" } 530 | } 531 | } 532 | 533 | 534 | After: 535 | 536 | { 537 | "_links": { 538 | "self": { "href": "/blog-post" }, 539 | "author": { "href": "/people/alan-watts" } 540 | }, 541 | "_embedded": { 542 | "author": { 543 | "_links": { "self": { "href": "/people/alan-watts" } }, 544 | "name": "Alan Watts", 545 | "born": "January 6, 1915", 546 | "died": "November 16, 1973" 547 | } 548 | } 549 | } 550 | 551 | 552 | 9. Security Considerations 553 | 554 | This media type does not contain executable content. 555 | 556 | 557 | 558 | 559 | 560 | Kelly Expires 21 April 2024 [Page 10] 561 | 562 | Internet-Draft JSON Hypertext Application Language October 2023 563 | 564 | 565 | The information contained in the media type does not necessarily 566 | require privacy or integrity services. 567 | 568 | The security considerations of the JSON format apply to this media 569 | type. 570 | 571 | 10. IANA Considerations 572 | 573 | No IANA actions required. 574 | 575 | 11. Normative References 576 | 577 | [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 578 | Requirement Levels", BCP 14, RFC 2119, 579 | DOI 10.17487/RFC2119, March 1997, 580 | . 581 | 582 | [RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, "Uniform 583 | Resource Identifier (URI): Generic Syntax", STD 66, 584 | RFC 3986, DOI 10.17487/RFC3986, January 2005, 585 | . 586 | 587 | [RFC4627] Crockford, D., "The application/json Media Type for 588 | JavaScript Object Notation (JSON)", RFC 4627, 589 | DOI 10.17487/RFC4627, July 2006, 590 | . 591 | 592 | [RFC5988] Nottingham, M., "Web Linking", RFC 5988, 593 | DOI 10.17487/RFC5988, October 2010, 594 | . 595 | 596 | [RFC6570] Gregorio, J., Fielding, R., Hadley, M., Nottingham, M., 597 | and D. Orchard, "URI Template", RFC 6570, 598 | DOI 10.17487/RFC6570, March 2012, 599 | . 600 | 601 | [RFC6906] Wilde, E., "The 'profile' Link Relation Type", RFC 6906, 602 | DOI 10.17487/RFC6906, March 2013, 603 | . 604 | 605 | Appendix A. Acknowledgements 606 | 607 | Thanks to Darrel Miller, Mike Amundsen, and everyone in hal-discuss 608 | for their suggestions and feedback. 609 | 610 | The author takes all responsibility for errors and omissions. 611 | 612 | 613 | 614 | 615 | 616 | Kelly Expires 21 April 2024 [Page 11] 617 | 618 | Internet-Draft JSON Hypertext Application Language October 2023 619 | 620 | 621 | Appendix B. Frequently Asked Questions 622 | 623 | B.1. How should a client know the meaning/structure/semantics/type of a 624 | resource? 625 | 626 | There are two main approaches to solving this problem. Both involve 627 | exposing additional documentation describing the resource which may 628 | be human and/or machine readable (i.e. an HTML page and/or a JSON 629 | Schema document). The difference between the two approaches is in 630 | where that URI is shared with the client, which is either: 631 | 632 | (1) The URI that was the preceding link relation type. 633 | 634 | (2) A 'profile' link from the resource itself. 635 | 636 | B.2. Where can I find libraries for working with HAL? 637 | 638 | A list of libraries is maintained here: 639 | https://github.com/mikekelly/hal_specification/wiki/Libraries 640 | 641 | B.3. Why are the reserved properties prefixed with an underscore? 642 | 643 | We elected for a prefix character to minimise risk of collisions with 644 | properties that represent the resource's state, and underscore was 645 | the character picked. 646 | 647 | Another reason for prefixing the reserved properties is to make it 648 | visually apparent that the reserved properties are distinct from 649 | standard properties belonging to the resource. 650 | 651 | B.4. Are all underscore-prefixed properties reserved? 652 | 653 | No, HAL only reserves the names detailed in this specification. 654 | 655 | B.5. Why does HAL have no forms? 656 | 657 | Omitting forms from HAL was an intentional design decision that was 658 | made to keep it focused on linking for APIs. HAL is therefore a good 659 | candidate for use as a base media type on which to build more complex 660 | capabilities. An additional media type is planned for the future 661 | which will add form-like controls on top of HAL. 662 | 663 | Author's Address 664 | 665 | Mike Kelly 666 | Stateless 667 | Email: mike@stateless.group 668 | URI: https://stateless.group/ 669 | 670 | 671 | 672 | Kelly Expires 21 April 2024 [Page 12] 673 | -------------------------------------------------------------------------------- /draft-kelly-json-hal.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 10 | ]> 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | JSON Hypertext Application Language 29 | 30 | 31 | Stateless 32 |
33 | mike@stateless.group 34 | https://stateless.group/ 35 |
36 |
37 | 38 | 39 | 40 | This document proposes a media type for representing resources and their relations with hyperlinks. 41 | 42 | 43 |
44 | 45 | 46 | 47 |
48 | 49 | There is an emergence of non-HTML HTTP applications ("Web APIs") which use hyperlinks to direct clients around their resources. 50 | 51 | The JSON Hypertext Application Language (HAL) is a standard which establishes conventions for expressing hypermedia controls, such as links, with JSON . 52 | 53 | HAL is a generic media type with which Web APIs can be developed and exposed as series of links. Clients of these APIs can select links by their link relation type and traverse them in order to progress through the application. 54 | 55 | HAL's conventions result in a uniform interface for serving and consuming hypermedia, enabling the creation of general-purpose libraries that can be re-used on any API utilising HAL. 56 | 57 | The primary design goals of HAL are generality and simplicity. HAL can be applied to many different domains, and imposes the minimal amount of structure necessary to cover the key requirements of a hypermedia Web API. 58 | 59 |
60 | 61 |
62 | The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL 63 | NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in 64 | this document are to be interpreted as described in . 65 |
66 | 67 | 68 |
69 | 70 | A HAL Document uses the format described in and has the media type "application/hal+json". 71 | 72 | Its root object MUST be a Resource Object. 73 | 74 | For example: 75 | 76 |
95 | 96 | Here, we have a HAL document representing an order resource with the URI "/orders/523". 97 | It has "warehouse" and "invoice" links, and its own state in the form of "currency", "status", and "total" properties. 98 | 99 |
100 | 101 |
102 | 103 | A Resource Object represents a resource. 104 | 105 | It has two reserved properties: 106 | 107 | "_links": contains links to other resources. 108 | "_embedded": contains embedded resources. 109 | 110 | 111 | 112 | All other properties MUST be valid JSON, and represent the current state of the resource. 113 | 114 |
115 | 116 |
117 | The reserved "_links" property is OPTIONAL. 118 | 119 | It is an object whose property names are link relation types (as 120 | defined by ) and values are either a Link 121 | Object or an array of Link Objects. The subject resource of 122 | these links is the Resource Object of which the containing 123 | "_links" object is a property. 124 | 125 | 126 | For a given link relation, servers SHOULD NOT change the property 127 | between a Link Object and array of Link Objects. Such changes SHOULD 128 | introduce a new link relation. This is to avoid breaking changes for 129 | existing clients coupled to the original choice, and is a 130 | consideration for any JSON based API. 131 | 132 |
133 | 134 |
135 | The reserved "_embedded" property is OPTIONAL 136 | It is an object whose property names are link relation types (as defined by ) and values are either a Resource Object or an array of Resource Objects. 137 | Embedded Resources MAY be a full, partial, or inconsistent version of the representation served from the target URI. 138 |
139 |
140 | 141 |
142 | 143 |
144 | 145 | A Link Object represents a hyperlink from the containing resource to a URI. It has the following properties: 146 | 147 |
148 | The "href" property is REQUIRED. 149 | Its value is either a URI or a URI Template . 150 | If the value is a URI Template then the Link Object SHOULD have a "templated" attribute whose value is true. 151 |
152 | 153 |
154 | The "templated" property is OPTIONAL. 155 | Its value is boolean and SHOULD be true when the Link Object's "href" property is a URI Template. 156 | Its value SHOULD be considered false if it is undefined or any other value than true. 157 |
158 | 159 |
160 | The "type" property is OPTIONAL. 161 | Its value is a string used as a hint to indicate the media type expected when dereferencing the target resource. 162 |
163 | 164 |
165 | The "deprecation" property is OPTIONAL. 166 | Its presence indicates that the link is to be deprecated (i.e. removed) at a future date. Its value is a URL that SHOULD provide further information about the deprecation. 167 | A client SHOULD provide some notification (for example, by logging a warning message) whenever it traverses over a link that has this property. The notification SHOULD include the deprecation property's value so that a client maintainer can easily find information about the deprecation. 168 |
169 | 170 |
171 | The "name" property is OPTIONAL. 172 | Its value MAY be used as a secondary key for selecting Link Objects which share the same relation type. 173 |
174 | 175 |
176 | The "profile" property is OPTIONAL. 177 | Its value is a string which is a URI that hints about the profile (as defined by ) of the target resource. 178 |
179 | 180 |
181 | The "title" property is OPTIONAL. 182 | Its value is a string and is intended for labelling the link with a human-readable identifier (as defined by ). 183 |
184 | 185 |
186 | The "hreflang" property is OPTIONAL. 187 | Its value is a string and is intended for indicating the language of the target resource (as defined by ). 188 |
189 |
190 | 191 |
192 | The following is an example document representing a list of orders 193 | 194 |
233 | 234 | Here, the order list document provides a "next" link directing to the next page, and a "find" link containing a URI Template which can be expanded with an 'id' variable to go directly to a specific order. 235 | It also has two embedded resources, "orders". Each of these has its own links to the associated "basket" and "customer" resources, and properties showing their "total", "currency" and "status". 236 | Additionally, the order list resource has its own properties "currentlyProcessing" and "shippedToday". 237 |
238 | 239 |
240 |
241 | The media type identifier application/hal+json MAY also include an additional "profile" parameter (as defined by ) 242 | HAL documents that are served with the "profile" parameter still SHOULD include a "profile" link belonging to the root resource. 243 |
244 |
245 | 246 |
247 |
248 | Each Resource Object SHOULD contain a 'self' link that corresponds with the IANA registered 'self' relation (as defined by ) whose target is the resource's URI. 249 |
250 | 251 |
252 | 253 | Custom link relation types (Extension Relation Types in 254 | ) SHOULD be URIs (or curies) 255 | that when dereferenced in a web browser provide relevant 256 | documentation, in the form of an HTML page, about the meaning and/or 257 | behaviour of the target Resource. This will improve the 258 | discoverability of the API. 259 | 260 |
261 | 262 |
263 | 264 | HAL etablishes a mechanism called "curies" which allows for link 265 | relation types that are compact and more human readable (eg. 266 | "acme:widgets"), whilst still offering a way that they MAY be expanded into a 267 | dereferencable URI providing documentation (eg. "https://docs.acme.com/relations/widgets") 268 | 269 | 270 | 271 | To this end, HAL documents have a reserved link relation type called "curies". 272 | 273 | 274 | 275 | HAL curies are established for a given Resource Object via an array of 276 | Link Objects with the "curies" reserved link relation type. These 277 | links contain a URI Template with the token 'rel', and are named via 278 | the "name" property. 279 | 280 | 281 | 282 | The following demonstrates the relation 283 | "https://docs.acme.com/relations/widgets" being abbreviated to 284 | "acme:widgets" using curies: 285 | 286 | 287 |
300 | 301 | 302 | HAL curies can be used to create versioned link relation types like so: 303 | 304 |
325 | 326 | 327 | In cases where an embedded Resource defines its own curies which conflict with 328 | those of its parent then, for links within this resource, these are 329 | overwritten and SHOULD take precedence over the curies of the parent. 330 | 331 | 332 |
333 | 334 |
335 | The "hypertext cache pattern" allows servers to use embedded resources to dynamically reduce the number of requests a client makes, improving the efficiency and performance of the application. 336 | Clients MAY be automated for this purpose so that, for any given link relation, they will read from an embedded resource (if present) in preference to traversing a link. 337 | To activate this client behaviour for a given link, servers SHOULD add an embedded resource into the representation with the same relation. 338 | Servers SHOULD NOT entirely "swap out" a link for an embedded resource (or vice versa) because client support for this technique is OPTIONAL. 339 | The following examples shows the hypertext cache pattern applied to an "author" link: 340 |
341 | Before: 342 | 350 | 351 |
352 |
353 | After: 354 | 370 | 371 |
372 |
373 | 374 |
375 | 376 |
377 | 378 | 379 | This media type does not contain executable content. 380 | 381 | 382 | 383 | The information contained in the media type does not necessarily require privacy or integrity services. 384 | 385 | 386 | 387 | The security considerations of the JSON format apply to this media type. 388 | 389 | 390 |
391 | 392 |
393 | 394 | No IANA actions required. 395 | 396 |
397 | 398 |
399 | 400 | 401 | 402 | &rfc2119; 403 | &rfc3986; 404 | &rfc4627; 405 | &rfc5988; 406 | &rfc6570; 407 | &rfc6906; 408 | 409 | 410 | 411 | 412 | 413 |
414 | Thanks to 415 | Darrel Miller, Mike Amundsen, and everyone in hal-discuss 416 | for their suggestions and feedback. 417 | 418 | The author takes all responsibility for errors and omissions. 419 |
420 | 421 |
422 |
423 | There are two main approaches to solving this problem. 424 | Both involve exposing additional documentation describing the resource which may be human and/or machine readable (i.e. an HTML page and/or a JSON Schema document). 425 | The difference between the two approaches is in where that URI is shared with the client, which is either: 426 | 427 | The URI that was the preceding link relation type. 428 | A 'profile' link from the resource itself. 429 | 430 | 431 |
432 | 433 |
434 | A list of libraries is maintained here: https://github.com/mikekelly/hal_specification/wiki/Libraries 435 |
436 | 437 |
438 | We elected for a prefix character to minimise risk of collisions with properties that represent the resource's state, and underscore was the character picked. 439 | Another reason for prefixing the reserved properties is to make it visually apparent that the reserved properties are distinct from standard properties belonging to the resource. 440 |
441 | 442 |
443 | No, HAL only reserves the names detailed in this specification. 444 |
445 | 446 |
447 | Omitting forms from HAL was an intentional design decision that was made to keep it focused on linking for APIs. HAL is therefore a good candidate for use as a base media type on which to build more complex capabilities. An additional media type is planned for the future which will add form-like controls on top of HAL. 448 |
449 |
450 | 451 |
452 |
453 | --------------------------------------------------------------------------------