Rest Endpoint Design

I have 6 endpoints that return 6 json response:

  • /cities/{id} return a json object: {
    “city”: “Orlando”,
    “altitude”: 10
    }
  • /cities return an array: [{
    “city”: “Orlando”,
    “altitude”: 70},
    {“city”: “New York”,
    “altitude”: 5}]

the other 4 endpoint are of the same kind (single object and array) but return totally different object/s.

Now I want to change the response payload, and envelope it in another object to add an hash of the original response and other info about the request that the user made. So for example, for the 2 endpoint above, I can have this:

{
  "uuid": "38271b3f-a097-43a6-82e9-ca482358a811",
  "date": "2024-09-10T15:25:04.771+02:00",
  "hash": -313570381,
  "data": {
    "city": "Orlando",
    "altitude": 10
  }
}

or

{
  "uuid": "38271b3f-a097-43a6-82e9-ca482358a811",
  "date": "2024-09-10T15:25:04.771+02:00",
  "hash": -313570381,
  "data": [{
    "city": "Orlando",
    "altitude": 70},
    {"city": "New York",
    "altitude": 5}]
}

or if I had an error, for example I will return 404 and a message:

{
  "uuid": "38271b3f-a097-43a6-82e9-ca482358a811",
  "date": "2024-09-10T15:25:04.771+02:00",
  "hash": -313570381,
  "data": {
    "errorMessage": "City not found"
  }
}

The main question is if it is ok that the field data could contain so different information (I made the example for 2 endpoints, but I have 6 of them that returns different objects or array).
The optional side question, maybe too broad, if my design is good and how I can improve it.

11

The main point I would call out is the change of type of the data element dependent on whether an error occurred. In the case of a valid response you get a list of maps:

  "data": [{

In the case of an error you get a map:

  "data": {

Technically you CAN get away with this, since you have different HTTP return codes (200 for normal and 4xx for errors) so you know before you try to pass the body what the structure of the data block is. This is consistent with a definition language like OpenAPI, since it allows totally different response bodies based on the HTTP return codes.


That said I would probably add a bit more standardization to my API’s and have three (optional) top level keys:

  • result – a map representing a single result.
  • results – a list of maps representing a list of result.
  • errors – a list of maps representing a list of error objects.

In most cases exactly one of those keys will be present in the response.

Note: I wouldn’t bother with an error key, since its fine to return a list with one error in it. Error handling is likely to be fairly standardized, where as you don’t want to have to keep stripping off an outer list from API calls that will always return one result.

Ideally all fields should be as consistent as possible, in terms of their data format and their purpose.

You have two different core types of response purpose, a successful result and an error. Rather than overloading the same field with the payload of both, it might be a good idea to have a separate ‘result’ and ‘error’ field, mutually exclusive — either ‘result’ or ‘error’ must be present, but never both or neither.

In terms of the ‘result’ field, you can standardise this by always returning an array, even when the request is guaranteed to only give one record. Thus, /cities returns a ‘result’ array of zero or more records, while /cities/{id} returns a ‘result’ array of zero or one records but never more than one. (I’m assuming here empty results with zero records are valid, and not treated as an error condition.)

You then just write your handler code to assume every result is an array, so you don’t need separate code paths to handle the returns from different types of request.

New contributor

javakx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật