Skip to main content

Using http Post to Retrieve Fields and Parameters.

If you are struggling with getting fields and parameters from http Post, here is some example code to help. We use these types of calls in DeployHub. We are happy to share some of the work our developers have done so you don’t have to work so hard.http post usage

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Engine
 */
public class Engine extends HttpServlet
{
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {
  String userName = request.getParameter("username");
 }
}

The request.getParameter("username"); will retrieve the username field from the payload of the POST or from the URL parameters. URL parameters will need to be encoded so the special characters come across correctly.

To test with curl using URL encoded parameters:

curl -X POST https://localhost:8080/myapp/Engine?username=joe+cool

where joe+cool has been encoded from joe cool (with a space)

To test with curl using data payload:

curl -X POST --data "username=joe cool" https://localhost:8080/myapp/Engine

For multiple parameters in the –data separate them with an &.