I got problem which get request POST parameter in router file

(PROEM Meas) #1

I am currently using play as restful by download sample api restful from Githube, I got problem with can’t get value from POST request parameter in router file.
Because my client UI Javascript framework require using POST every time request API to server, that mean no HTTP GET as url end point, so I need get some param to route which controller and method to be execute. I need get request in route file for match case. After do many research Play not recomment get request in router file because related request need stream via AKKA in controller file with method Action and implicit request.
This is URL Github router file that i want custom get POST param value:


(Marcos Pereira) #2

Hi @meas.proem,

I don’t know if I fully understood your question, but you can also have URL parameters in routes using POST HTTP method. For example:

POST  /v1/articles/:articleId/comments    controllers.Articles.addComment(articleId: Long)

Or when using SIRD:

case POST(p"/articles/$id/comments") => Action {
  Results.Ok(s"Article $id")

It is that what you want?


(PROEM Meas) #3

Dear @marcospereira

Thank for reply and sorry not clear my question, I want get value from json Request PlayLoad.
That mean when I make request to server api with http POST I will include with some json. Can i get that json in router ?

Best Regards

(Marcos Pereira) #4

Hey @meas.proem,

Got it. So, there is an example here:


You need to have a regular action that uses parse.json (see our docs about body parsers) which will then process the POST payload as JSON. Given that, you won’t have any JSON declaration in your route, but the action instead. For example, considering the action at the documentation:

def savePlace = Action(parse.json) { request =>
  val placeResult = request.body.validate[Place]
    errors => {
      BadRequest(Json.obj("status" ->"KO", "message" -> JsError.toJson(errors)))
    place => {
      Ok(Json.obj("status" ->"OK", "message" -> ("Place '"+place.name+"' saved.") ))

You can have a regular route like:

POST /places/save    controllers.Places.savePlace