Search code examples
angular8liferay-7.3

Liferay - how to retrieve body content of POST request in "serveResource" method


I am issuing a POST request from my Angular 8 Liferay 7.3 Portlet and trying to get its contents in the serveResource method in the MVCResourceCommand class.

The frontend JS looks like this:

submit() {
    let message = {
        type: "form",
        body: this.model
      };
    
    this.http.post<any>("http://my/url", message).subscribe(response => {
        console.log(JSON.stringify(response));
    })
  }

The meesage hits the endpoint, so the backend is well configured. Based on this, this and this I have also set the "com.liferay.portlet.requires-namespaced-parameters=false" property in order namespaces not to be an issue - so, basically, every point in the check list is done and the request body should be accessible. Every post I read so far is talking about getting the request info with ParamUtil.getString(uploadRequest, "text");, but I am not sure how this should work if the body is a JSON object - I mean, how should I retrieve the value if the POST body looks like this:

{
    "firstVal": "abc",
    "secondVal": "def",
    "another": {
        "objectVal1": 1,
        "objectVal2": 2
    }
}

Solution

  • Thanks to this post, I only came to this piece of code, which retrieves the body as a String:

    String body = PortalUtil.getHttpServletRequest(resourceRequest).getReader().lines()
                    .collect(java.util.stream.Collectors.joining(System.lineSeparator()));
    

    Even though I solved my problem, I would like to know if there are other ways to do this.