Erick Fleming Erick Fleming - 1 year ago 138
Java Question

Servlet parameters and doPut

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?

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download