简体   繁体   中英

Add elements to existing REST-api

Is it considered good practice to add elements to a response from an 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? 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? 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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