开发者

Is there a preferred way to list errors in response to a API request?

开发者 https://www.devze.com 2023-03-16 04:06 出处:网络
I\'m building a JSON REST API. Some of the API actions require attributes. In case a API request to those actions is made without all the required attributes, the API returns a 4xx HTTP code and the l

I'm building a JSON REST API. Some of the API actions require attributes. In case a API request to those actions is made without all the required attributes, the API returns a 4xx HTTP code and the list of missing attributes in JSON format.

For now, this JSON response only contains an object with the "errors" k开发者_如何学编程ey storing an array of error messages. For example:

{"errors":["'first_name' attribute is missing","'title' attribute is missing"]}

So the API consumer just have to check if the "errors" key is null or contains an array of error messages.

Do you think that might be enough? This is a vague question, I'm looking for best/recommended practices to report such errors, to make the consuming of the API more natural and easy.

0

精彩评论

暂无评论...
验证码 换一张
取 消