根据HTTP/1.1规范:
该
POST
方法用来请求原始服务器接受被附在请求由标识的资源的新下属实体Request-URI
的Request-Line
换句话说,POST
用于创建.
该
PUT
方法请求将所包含的实体存储在提供的实体下Request-URI
.如果Request-URI
引用已经存在的资源,则封闭的实体应该被视为驻留在源服务器上的实体的修改版本.如果Request-URI
未指向现有资源,并且该URI能够被请求用户代理定义为新资源,则源服务器可以使用该URI创建资源.
也就是说,PUT
用于创建或更新.
那么,应该使用哪一个来创建资源?或者需要支持两者?
尝试使用HttpServlet#doPut从PUT请求获取参数:
public void doPut(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
// name is null
}
Run Code Online (Sandbox Code Playgroud)
使用curl发送请求:
curl -X PUT \
--data "name=batman" \
--header "Content-Type: text/plain" http://localhost:8080/sample.html
Run Code Online (Sandbox Code Playgroud)
使用doGet和GET curl请求可以正常工作.我错过了什么吗?