views:

851

answers:

2

Trying to get parameters from a PUT request using HttpServlet#doPut:

public void doPut(HttpServletRequest request, HttpServletResponse response) {
    String name = request.getParameter("name");
    // name is null
}

Using curl to send the request:

curl  -X PUT \
      --data "name=batman" \
      --header "Content-Type: text/plain" http://localhost:8080/sample.html

works fine with using doGet and GET curl request. Am I missing something?

+1  A: 

What do you mean by doPut() is not working? As far as I know, it doesn't work like doGet() or doPost(), where you have request parameters and stuff.

PUT can be used to put something on the server. In particular, the PUT operation allows a client to place a file on the server and is similar to sending a file by FTP. Check out this example, I found on JGuru.

->GET /file.dat HTTP/1.1

<-HTTP/1.1 404 Not Found

->PUT /file.dat HTTP/1.1
Content-Length: 6
Content-Type: text/plain

Hello!

<-HTTP/1.1 200 OK

->GET /file.dat HTTP/1.1

<-HTTP/1.1 200 OK
Content-Length: 6
Content-Type: text/plain

Hello!
Adeel Ansari
A: 

Based on comments and further research I realized that the Servlet cannot assume anything about the data being put onto the server and therefore, will not parse name/value pairs.

The following solution seems to be the proper way to handle any data passed via PUT, and can be parsed as XML, Name/Value, or whatever.

BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream)

String data = br.readLine
efleming969
Why are you trying to use it in a way its not meant for? However you can play around with the thing as you please.
Adeel Ansari
I'm running into the same problem: i'm trying to implement a restfull service, PUT is meant to replace the resource. Jetty makes all data available in the request parameters map, tomcat however doesn't..
Andrej