Alternative bodies for HTTP PUT

2019-08-10 05:43发布

问题:

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

回答1:

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.



回答2:

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.



标签: http rest put