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?
To retrieve the body of the POST request sent to the handler, we'll use the @RequestBody annotation, and assign its value to a String. This takes the body of the request and neatly packs it into our fullName String. We've then returned this name back, with a greeting message.
A request body is data sent by the client to your API. A response body is the data your API sends to the client. Your API almost always has to send a response body. But clients don't necessarily need to send request bodies all the time.
With Play Framework 2.3 it is possible to get raw json text even is Content-Type header is application/json
def postMethod = Action(parse.tolerantText) { request =>
val txt = request.body
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With