Sunday, March 20, 2011

Use DTD to describe XML returned in RESTful service?

Given the service:

> GET /hotel

< HTTP/1.1 200 OK
< <hotel>
<   <a>aaa</a>
<   <b>aaa</b>
>   <c>aaa</c>
< </hotel>

Should one reference a DTD in the XML returned from the server?

Would this better allow a client to validate the response?

From stackoverflow
  • It's certainly good practice to reference your DTD/schema, and yes, it will allow clients to validate the response, if they choose to. They often won't.

    Marcus : That's what I was thinking. Although I haven't seen too many people do this. Our app is internal (just a few clients) so not sure if it's overkill.
    swillden : It doesn't cost much. IMO, it's better than relying on content-type.
  • Considering the markup you have chosen appears to be custom to your requirements then I would expect to see a media-type such as

    application/vnd.yourcompany.hotel+xml
    

    in your content-type HTTP header. Based on this content-type, the client will know whether it has the knowledge to process this representation.

    Marcus : What does "vnd" refer to?
    serialseb : it's the identifier of the vendor tree, which you can use without having to register your media type with the IANA.
    Marcus : Not exactly clear how clients use this in their processing.
    Darrel Miller : What I do is have the client detect the media-type and then pass the response body to a handler that knows how to decode that representation. In some cases it causes the application to display a new UI containing the representation, on other cases it simply returns an object that contains the information from the representation. It depends completely on what the media-type is and the context of the originating request.
    Darrel Miller : I try and make my REST client applications data driven. The example I use to describe it is to compare loading an application like MS Excel and then doing file open and retrieving an excel file. This is how most client applications work, they create some kind of UI and then go off and get data to put in that UI. The other approach is where you go to Windows Explorer and double-click on an Excel file. This is a data driven approach. The file extension determines how the data will be rendered. In a REST client, the returned media-type can be used to drive the application.
  • The media-type header can help your client know what kind of document to use. It can also help you version the service by having different document types for each version.

    application/vnd.yourcompany.hotelv1+xml
    application/vnd.yourcompany.hotelv2+xml
    

    etc.

    The client can also specify which kind of document it would like back in the Accepts header.

0 comments:

Post a Comment