Alternative bodies for HTTP PUT

跟風遠走 提交于 2020-01-06 01:51:07

问题


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.



来源:https://stackoverflow.com/questions/982351/alternative-bodies-for-http-put

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!