简体   繁体   English

RESTful Web服务:记录同一资源的XML

[英]RESTful Web Services: Documenting XML for the same resource

I'm using the W3C XML Schema (XSD) for documenting resources. 我正在使用W3C XML架构(XSD)来记录资源。 Today, XML representation is returned in a handful of resources. 如今,在少数资源中返回了XML表示形式。 A POST operation on one of my resources requires XML from the client. 对我的资源之一进行POST操作需要客户端提供XML。 The response for this resource is XML. 此资源的响应是XML。 My question is as follows: 我的问题如下:

a) Should I create one XSD to handle request (POST), response for the resource? a)我应该创建一个XSD来处理资源的请求(POST),响应吗?

b) If a single XSD is being used, how should I differentiate between request (POST) and responses? b)如果使用单个XSD,我应该如何区分请求(POST)和响应? One thought surrounds: 一个想法围绕着:

<stream>
  <request>
    <items>xxx</items>
  </request>
  <response>
    <stuff>xxx</stuff>
  </response>
</stream>

Trouble is, I fear the request/response tags reflect an RPC style approach. 问题是,我担心请求/响应标签会反映RPC样式的方法。

c) With regards to POST operations, I'd like to inform client that XML is required and is only allowed during POST operations. c)关于POST操作,我想通知客户端XML是必需的,并且仅在POST操作期间允许。 I suspect if this requirement is violated my best option is to return a 400 bad request? 我怀疑是否违反了此要求,我最好的选择是返回400个错误的请求? Beyond that, I think my only option with respect to my XSD is 'documenting via comments the requirement'? 除此之外,我认为关于XSD的唯一选择是“通过注释记录需求”? Just trying to get a feel for any XSD design options I should consider besides comments. 只是尝试了解任何XSD设计选项,除了评论之外,我还应该考虑一下。 Of course the challenge with one XSD is a response to a GET operation - which does not require a XML from the client - will require the client to 'ignore' the POST content in the XML. 当然,一个XSD面临的挑战是对GET操作的响应-不需要客户端的XML-将要求客户端“忽略” XML中的POST内容。

Thanks in advance 提前致谢

a) creating one XSD and defining multiple types within that XSD is fine. a)创建一个XSD并在该XSD中定义多种类型是可以的。 It seems reasonable to me to define a request/response pair in a single XSD. 在我看来,在单个XSD中定义一个请求/响应对是合理的。 Keep in mind the consumers or users of the XSD. 请记住XSD的使用者或用户。 Who will it be? 会是谁 Do you expect the users of your API to employ that XSD to construct apps, or will it be used only by the server (Receiving) side in order to validate requests as they come in? 您是否希望您的API的用户使用该XSD来构建应用程序,还是仅在服务器(接收)端使用它来验证请求的出现?

In many cases, the messages that get exchanged in a REST api are not formally defined in a schema document. 在许多情况下,在架构api中没有正式定义在REST api中交换的消息。 Instead they are documented in an html page, and then companies provide an API test page, where they can see example requests and responses. 而是将它们记录在html页面中,然后公司提供API测试页面,他们可以在其中查看示例请求和响应。 It's a very pragmatic way of documenting APIs. 这是记录API的非常实用的方式。 For an example of this, see https://api.stackexchange.com/docs/answers . 有关此示例,请参阅https://api.stackexchange.com/docs/answers


b) To differentiate between request and response I suppose you would use distinct element types in the XSD. b)为了区分请求和响应,我想您将在XSD中使用不同的元素类型。 Maybe I am not understanding the question. 也许我不明白这个问题。


c) Returning 400 is entirely reasonable, if the request is the wrong content-type, does not include an XML document, or the XML document does not conform to the required schema. c)如果请求的内容类型错误,不包含XML文档或XML文档不符合要求的架构,则返回400是完全合理的。 If you do return a 400, it's a good idea to also return a message body, with an XML document that gives more information: 如果确实返回400,则最好还返回消息正文,并带有提供更多信息的XML文档:

<response> 
  <error>
    <code>101.4.c</code>
    <description>your request did not include an XML document</description>
    <link>http://example.com/errorinfo?code=101.4.c</link> 
  </error>
</response> 

For more on REST API design, see http://dinochiesa.net/?p=192 有关REST API设计的更多信息,请参见http://dinochiesa.net/?p=192

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM