Link headers vs link elements for RESTful JSON

后端 未结 5 1600
余生分开走
余生分开走 2021-02-01 04:09

When building a RESTful / hypermedia API with JSON resources, it seems I have two options for specifying the hypermedia relationships between resources.

  1. Embed t

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 04:54

    If you want your links to be processed by HTTP intermediaries then you should definitely use Link Headers. One example of this is Linked Cache Invalidation :

    http://tools.ietf.org/html/draft-nottingham-linked-cache-inv-01

    If you just want to expose links to your clients you're better off putting them in the entity in order to take advantage of links within nested elements :

    {
        'item': [
            { 'name': 'fork',  'href': 'http://example.com/item/1' },
            { 'name': 'spoon', 'href': 'http://example.com/item/2' },
            { 'name': 'spork', 'href': 'http://example.com/item/3' }
        ],
        'href': 'http://example.com/items'
    }
    

提交回复
热议问题