编写REST API响应的最佳实践是什么?

Dee*_*jay 0 api rest cakephp

我想知道用于编写REST API响应的实践: -

{
    "Headline": {
        "score": 10,
        "summary": "Public Url Description.",
        "status": 1,
        "description": "Perfect! The URL contains your name"
    },
    "Profile Picture": {
        "score": 10,
        "summary": "Profile Picture Description.",
        "status": 1,
        "description": "Amazing Job, Now you are more approachable."
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

{
    "status": 200,
    "response": {
        "Headline": {
            "score": 10,
            "summary": "Public Url Description.",
            "status": 1,
            "description": "Perfect! The URL contains your name"
        },
        "Profile Picture": {
            "score": 10,
            "summary": "Profile Picture Description.",
            "status": 1,
            "description": "Amazing Job, Now you are more approachable."
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我是编写API的新手.我想知道哪种做法可以帮助我编写更好的API.

我使用CakePHP作为框架.

我应该在API响应中添加响应代码吗?

小智 6

第二个响应复制已经处于HTTP响应状态的信息.HTTP状态将是200 OK服务器设置的任何其他HTTP状态代码.如果你不小心保持两个值相等,那么在响应体中复制它只会导致混淆.

使用第一个表格.

  • 确切地说,我已经回答了这个问题:不要在响应正文中发送状态代码.在HTTP Resonse中设置状态. (3认同)