Design return values for RESTful API

后端 未结 2 1239
Happy的楠姐
Happy的楠姐 2021-01-23 03:18

There is a lot of documentation for RESTful services. I need more Information about the returning representations.

What is the best way for design the return value for R

2条回答
  •  清歌不尽
    2021-01-23 03:45

    Using query parameters to request a subset of fields

    You can return the same representation in both cases and support filtering the fields to be returned with a query parameter:

    GET /api/cars?fields=color,year HTTP/1.1
    Host: example.com
    Accept: application/json
    

    Using custom media types to return a predefined set of fields

    Another approach is to define a custom media type for a partial representation of your resource.

    For example, you could use one of the following (or both) media types to retrieve a full representation of a collection of resources:

    GET /api/cars HTTP/1.1
    Host: example.com
    Accept: application/json
    
    GET /api/cars HTTP/1.1
    Host: example.com
    Accept: application/vnd.company.full+json
    

    And the following to return a partial representation for your resource (the fields you will include in the partial representation are up to you):

    GET /api/cars HTTP/1.1
    Host: example.com
    Accept: application/vnd.company.partial+json
    

提交回复
热议问题