javapostplayframeworkplayframework-2.0

Retrieve the request body string sent in POST request in play framework java


I'm using play framework in Java. I want to retrieve the entire request body sent in a POST request to the play server. How can I retrieve it?


Solution

  • Take a look into play.mvc.Http class, you have some options there (depending on data format) i.e.

    RequestBody body = request().body();
    MultipartFormData formData = request().body().asMultipartFormData();
    Map<String, String[]> params = request().body().asFormUrlEncoded();
    JsonNode json = request().body().asJson();
    String bodyText = request().body().asText();
    

    You can test request().body().asText() i.e. using cUrl from commandline:

    curl  -H "Content-Type: text/plain" -d  'Hello world !' http://domain.com/your-post-action
    

    ... or using some tool, like browser plugin: https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo