tags:

views:

127

answers:

2

Dear list,

I'm developing a REST-ful webservice, and I have a question about the HTTP PUT method.

I want to allow people to submit content using a application/form-data request body. However, the default response will be in application/xml.

Is this acceptable?

Evert

A: 

RESTful services should use the correct HTTP method (GET,HEAD,PUT,DELETE or POST) for the action, ensure that any scoping information is contained in the URI and ensure that the HTTP message envelope does not contain another envelope i.e. SOAP.

Roy Fieldings 2000 Ph.D. dissertation: Architectural Styles and the Design of Network-Based Software Architectures forms the foundation of REST.

Dave Anderson
Interesting article, but it doesn't answer my question..Example:I PUT a new article on /articles/firstpost, I get the option to use a content-type of either application/xml or application/form-data. I choose the later.Then I do a GET on the article, and I receive the application/xml representation.. Is this ok?
Evert
Sorry still rather new to REST myself but as I understand it this really depends on how the XML will be consumed by the client. The message entity should be in a data format that is most appropriate for the client and not require specific parsing for the problem as this would couple the client and the service together. Here is a post that discusses this some more; http://www.peej.co.uk/articles/rest-data-formats.html. What were your reasons for thinking application/xml might not follow REST principles?
Dave Anderson
+2  A: 

Content types are only important within the scope of a single request. All they do is describe the format of the content that is being sent.

Your web service should provide the response most acceptable to the client request that it is capable of providing. The client request should include an Accept header that describes the acceptable content types. If your service can't provide any of the content types in this header then return 406 Not Acceptable

In your situation, if your client GET requests include application/xml in the Accept header then it is fine to respond with application/xml, regardless of any PUT request made on the requested resources.

EDIT:

The status code definition for 406 Not Acceptable includes a note with the following:

Note: HTTP/1.1 servers are allowed to return responses which are not acceptable according to the accept headers sent in the request. In some cases, this may even be preferable to sending a 406 response. User agents are encouraged to inspect the headers of an incoming response to determine if it is acceptable.

So you can return application/xml whenever you want.

rojoca