Reputation: 4984
I try to write an action that calls several other actions of other controllers. Those actions expect a JSON as request body:
JsonNode jsonNode = request().body().asJson();
How can I call another action with a specific request body (JSON)?
Restriction: I can not modify the other controllers.
Upvotes: 0
Views: 274
Reputation: 93
I haven't tested this but I think this should do the trick. This version is for scala, I am not familiar with Play Java.
def actionJson: Action[JsValue] = Action(parse.json) { request =>
Ok("json body")
}
def actionAny: Action[AnyContent] = Action.async { request =>
actionJson(request.map(_.asJson)).run
}
Upvotes: 0
Reputation: 48213
I recommend to use the Reverse Routing feature of play, get a Call
to the destination Action Method
, convert it to a url and use the WSClient
in order to send your request. In scala, say you're gonna send a POST
request to an action method in Application
controller named index
:
val url = routes.Application.index().absoluteURL
wsClient.url(url).post[JsValue](json)
Same ideas apply to Java API but i'm not quite familiar with the details
Upvotes: 1