1

How can I print the Request Payload data to the page via JSTL?

Post Data

In the image above I am sending a post to a jstl controller. Now I can access the query string parameters easily e.g. ${param.test} will print out the test parameter.

But I am not sure how to access the data in the Request Payload I have tried the ${requestScope} object ${requestScope.PAY} but the data does not seem to be there.

Is there an accessible object that holds this data? If not what is the best work around?

Update

I don't know how relevant, but I am using angular's $http.post for the call.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Trevor
  • 16,080
  • 9
  • 52
  • 83
  • try same way `${param.PAY}` – jmj Aug 22 '14 at 16:41
  • @JigarJoshi unfortunately that does not work for me either.. Do you know why that might be the case? – Trevor Aug 22 '14 at 16:42
  • Check out [this question](http://stackoverflow.com/questions/14525982/getting-request-payload-from-post-request-in-java-servlet), I think OP there was in the same case as you. There's also a solution (before using the elaborated example, please read the comments belwo wrt encoding issues), however you'll have to code that solution in a servlet as adding Java code to JSP's should be avoided. – fvu Aug 22 '14 at 16:50
  • 1
    To clarify previous comment a bit: what you see here as "Request Payload" is actually the body of the request, so you need a method to extract the body from the request. It's not accessible via the headers or parameters. – fvu Aug 22 '14 at 16:58
  • @fvu is this something that could be addressed by the way the post is being initiated? I was looking at a comment from the link you gave: `It'd be interesting to see the JavaScript code sending the request. It's apparently composing the request parameters in a wrong way. – BalusC` If possible I would want to correct the issue on the post side before going to a `servlet` solution. I appreciate the info – Trevor Aug 22 '14 at 17:03
  • `This seems relevant as well` http://stackoverflow.com/questions/12190166/angularjs-any-way-for-http-post-to-send-request-parameters-instead-of-json – Trevor Aug 22 '14 at 17:10

2 Answers2

2

Two options:

1) When you send the request, send it as content-type equal to "application/x-www-form-urlencoded" instead of "text/json", and then you can access the parameters with ${param.PAY} just as if it was a query string parameter. The "application/x-www-form-urlencoded" is the default encoding when you POST an HTML form.

-or if that's not an option because you don't control the request side-

2) You will need to use a servlet or JSP scriptlet to access the contents, as in this question: HttpServletRequest get JSON POST data

Community
  • 1
  • 1
alfreema
  • 1,308
  • 14
  • 26
  • Thanks for the info, unfortunately just changing the `content-type` did not work for me I also had to perform a transformation on the data using `jQuery's $.param()` function as shown here: http://stackoverflow.com/questions/12190166/angularjs-any-way-for-http-post-to-send-request-parameters-instead-of-json – Trevor Aug 22 '14 at 17:30
1

What you want to do is to transform your post request in Angular to application/x-www-form-urlencoded. So in your application you'll do something like this.

var app = angular.module('myAapp', [])
    app.config(function($httpProvider) {
        $httpProvider
        .defaults
        .headers
        .post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

    $httpProvider.defaults.transformRequest = [function (data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? jQuery.param(data) : data;
    }];
    })

This should work.

Robert Wilson
  • 659
  • 1
  • 12
  • 28
  • You are using the jQuery.param function, for those looking for a javascript alternative. see: http://stackoverflow.com/a/5505137/1022305 – Trevor Aug 11 '15 at 17:18