小编Sek*_*des的帖子

REST API错误代码返回结构

我正在编写一个REST API,我偶然发现了一个问题.返回验证错误的最佳方法是什么.

到目前为止,我已经将转储的错误消息返回到一般错误代码中(例如,假设请求错误)

{
    "status": 400,
    "error": {
        "code": 1, // General bad request code
        "message": [
                "The Key \"a\" is missing",
                "The Key \"b\" is missing",
                "The Key \"c\" is missing",
                "Incorrect Format for field \"y\""
         ]
    }

)
Run Code Online (Sandbox Code Playgroud)

我已经研究了一些关于良好的API响应应该是什么样子的更多信息,我想到了以下几个选项:

  1. 停止第一次遇到的错误并返回带有特定错误代码的响应

    {
       "status": 400, //Same as the HTTP header returned
       "error" {
            "code": 1, // Specific field validation error code
            "message": "Field \"x\" is missing from the array structure",
            "developer_message": "The request structure must contain the following fields {a,b,c{x,y,z}}", …
    Run Code Online (Sandbox Code Playgroud)

api rest json http

26
推荐指数
2
解决办法
1万
查看次数

标签 统计

api ×1

http ×1

json ×1

rest ×1