我正在一个项目中负责前端开发,还有另外一位负责构建API。我提议所有请求都应该按照以下JSON格式进行:
{
"success": true, // true/false
"message": null, // a string if success==false indicating the error
"data": {} // The actual data in the response
}
他们更关心使API更加符合RESTful规范,而不是使用“message”字段。他们建议在状态码消息中发送消息,例如在HTTP头中:
HTTP/1.1 401 Authentication Failed for john.smith@example.com. Please log in again.
前端将会以弹窗或其他方式显示“john.smith@example.com认证失败,请重新登录。”
我担心有长度限制,但是我没有找到任何指示没有最大长度的东西。我们是否应该确保将这些消息保持在最小长度?有没有不这样做的好理由,而是将其作为内容发送(JSON或纯文本)?