2

I have following request body.

BODY: code=471b001b-432f-3172-b59f-2b03c7847cf6&client_secret=AtO0zxf62KoYasYTobOGRXVRZXsa&grant_type=authorization_code&client_id=4rYClwGnY4CE_XXAkMCoWuI4mnIa&redirect_uri=https%3A%2F%2Fop.certification.openid.net%3A60746%2Fauthz_cb

I need to read the client_secret parameter from the request body. I'm using below code to read the request body.

Scanner scanner = null;
        try {
            scanner = new Scanner(request.getInputStream());
        } catch (IOException e) {

        }
        while (scanner.hasNextLine()) {
            stringBuilder.append(scanner.nextLine());
        }
        String requestBody = stringBuilder.toString();

I can create a logic to get the value of client_secret parameter from the requestBody String. But I need to know whether we have a direct way to read parameters from a HTTPRequest Body.

Any suggestion will be highly appreciated.

Thanks.

Hasanthi
  • 1,251
  • 3
  • 14
  • 30

1 Answers1

0

ServletRequest (and by extension, HttpServletRequest) (1) objects have built-in support to get the parameters of the request.

You can get a specific parameter by using :

String paramValue = request.getParameter('parameterName');

The return type will always be String. Note that this method also works with GET requests (right now I guess you are treating a POST request, as the parameters are within the request's body).

You might also take a look at getParameterMap, getParameterValues and getParameterNames, to see if that can be of use.

HttpServletRequest Javadoc : http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html

ServletRequest Javadoc : http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html

(1) : I assume that is the type of your object, according to the getInputStream(), please correct me if I'm wrong.

Ephi
  • 346
  • 2
  • 12