I'm making an API to allow others to integrate our services into their own systems.

Finding the best way to formulate a response is not easy - I've seen so many different ways of doing it. Many times it is done quite poorly and myself and others have a hard time using it.

This is how I've done it:

<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>

If there is an error, then the HttpResponse will give the proper code to signalize this and the data will contain a descprition of the error, like this:

<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>

My questions are simply: does this response make any sense?

Is there a scenario that I'm not taking into consideration?

Will people have problems retrieving data from a response like this?

How else can I do this?

BTW: I will not use JSON, end of discussion!

有帮助吗?

解决方案

New way of showing errors based on comments.

I don't want move error up and replace "data", because I want to keep the possibility of having multiple errors. Instead I changed "data" to "errors".

<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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top