简体   繁体   English

向现有 REST-api 添加元素

[英]Add elements to existing REST-api

Is it considered good practice to add elements to a response from an api?将元素添加到来自 api 的响应是否被认为是好的做法?

If the existing response looks like this:如果现有响应如下所示:

{
"Animal": "Dog", 
"nrOfZoomsADay": 300
}

Would if be considered good practice to add elements to this allready in use REST-client response?如果将元素添加到此已使用的 REST 客户端响应中,是否会被视为良好做法? Or should you create a new endpoint?还是应该创建一个新端点? Keep in mind that this is an old endpoint, where you have no control over users.请记住,这是一个旧端点,您无法控制用户。

{
"Animal" : "Dog", 
"nrOfZoomsADay": 300,
"lastSeen": "Couch",
"favoriteToy: "Teddy Bear"
}

First of all, what is the API-mapping/request-method?首先,API 映射/请求方法是什么? Do the newly added attributes in the response make sense and would a client expect to have them in the response body?响应中新添加的属性是否有意义,客户是否希望将它们包含在响应正文中?

Generally, if youre the only client using your API-methods(for example web application with a REST backend) then theres no problem modifying the response body, because your able to adjust to it on your client side.通常,如果您是唯一使用 API 方法的客户端(例如带有 REST 后端的 Web 应用程序),那么修改响应主体没有问题,因为您可以在客户端对其进行调整。

If you have clients which you dont know and dont have control over like you said, you should make sure that your API response modification doesnt break their currently working program.如果您有您不认识并且无法控制的客户,那么您应该确保您的 API 响应修改不会破坏他们当前正在运行的程序。 In your case, adding new attributes to a response body doesnt break their functionality like removing existing ones would for example, so that shouldnt be a problem.在您的情况下,向响应主体添加新属性不会破坏它们的功能,例如删除现有属性,因此这不应该成为问题。 They will just automatically ignore the newly added attributes.他们会自动忽略新添加的属性。 The only downside is more traffic.唯一的缺点是更多的流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM