开发者

how to structure REST API XML response

I'm trying to define how to return XML content from my web API.

Should the root element of the web server response always be called "Content", or should I define a (possibly) different root element for each of the API calls' repon开发者_如何学JAVAses? Or do I even need a root key at all?


I would stay away from trying to define it on your own and go with something standard like wrapping around in Atom or similar. As I mentioned in another answer, there is precedent for this type of thing (no matter whether you use JSON or XML).


This sort of question demonstrates one of the many reasons JSON is far superior to XML for data. You don't have to name everything.

Recommendation: just use JSON.


The API we maintain has a single constant root element. Really it is up to you. Other alternatives might be a different roots per data return type, different roots for classes, or as you say, different roots per function.

Our API is structured something like this (not that it is a standard or anything):

<product name common root>
<class name>
<function return data>...</function return data>
</class name>
</product name common root>

We also allow batch calling, so this helps with determining which results are which.

<product name common root>
<class name>
<function1 return data>...</function1 return data>
</class name>
<class2 name>
<function2 return data>...</function2 return data>
</class2 name>
</product name common root>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜