我的API响应是否足够好?

时间:2013-10-31 12:57:37

标签: xml api response httpresponse error-code

我正在制作一个API,允许其他人将我们的服务集成到他们自己的系统中。

找到制定回应的最佳方法并不容易 - 我已经看到了很多不同的做法。很多时候,它做得很差,而我和其他人则很难使用它。

我就是这样做的:

<xml>
    <HttpResponse>200</HttpResponse>
    <data>
        <report>
            <id>200</id>
            <date>07.03.2013 11:13:00</date>
            <title>Fake report 1</title>
            <content>Lorem ipsum dolor sit amet.</content>
        </report>
        <report>
            <id>448</id>
            <date>10.04.2013 12:13:34</date>
            <title>Fake report 2</title>
            <content>Lorem ipsum dolor sit amet.</content>
        </report>
        <report>
            <id>927</id>
            <date>25.10.2013 11:49:34</date>
            <title>Fake report 3</title>
            <content>Lorem ipsum dolor sit amet.</content>
        </report>
    </data>
</xml>

如果有错误,那么HttpResponse将给出正确的代码来表示这一点,并且数据将包含错误的描述,如下所示:

<xml>
    <HttpResponse>418</HttpResponse>
    <data>
        <error>
            <code>AB43</code>   /* <<-- this code says what I can search for in the code to find the exact process/line where it failed */
            <description>You are the one who messed up!!!</description>
        </error>
    </data>
</xml>

我的问题很简单:这种回应是否有意义?

是否存在我未考虑的情况?

人们在从这样的响应中检索数据时会遇到问题吗?

我还能怎么做?

BTW:我不会使用JSON,讨论结束!

1 个答案:

答案 0 :(得分:0)

根据评论显示错误的新方法。

我不希望移动错误并替换“数据”,因为我想保留多个错误的可能性。相反,我将“数据”更改为“错误”。

<xml>
    <HttpResponse>418</HttpResponse>
    <errors>
        <error>
            <code>AB43</code>
            <title>document_title_too_long</title>
            <description>The title is limited to 64 characters, yours was 73. </description>
        </error>
        <error>
            <code>AC85</code>
            <title>document_no_category</title>
            <description>You must select a category to save the document. </description>
        </error>
    </errors>
</xml>
相关问题