What is a right way for REST API response?

What is a best practice for REST API response structure and layout?

Example of scrath:

Success response :

{
    "status": "success",
    "data": # some data here
}

Fail response :

{
    "status": "fail",
    "data": {
                "code": # some error code,
                "message": # some error explaining message
            }
}

There is many ways to design you API response. It is conditional to your architecture, technology, and other aspects.

Based on your example, I would respond this way

Successful request:

{
    "status": "success",
    "data": {
            /* Application-specific data would go here. */
    },
    "message": null /* Or optional success message */
}

Failed request:

{
    "status": "error",
    "code": 404,
    "data": null, /* or optional error payload */
    "message": "Error xyz has occurred"
}

For more info about this topic take a look at this links

Standard JSON API response format?

Best Practices for Designing a Pragmatic RESTful API

REST API Error Codes 101

链接地址: http://www.djcxy.com/p/48078.html

上一篇: json响应结构标准

下一篇: 什么是REST API响应的正确方法?