Scala/Java HTTP parse POST data form-encoded arrays

929 views Asked by At

I use Finatra. If I send POST data of the kind application/x-www-form-urlencoded; charset=UTF-8 where the data is of the kind

options[0][name]:option1
options[0][value]:1
options[1][name]:option2
options[1][value]:2

what is a good way to get a List of (name, value) on the server?

1

There are 1 answers

0
eodgooch On

This can be improved but should do the job. Convert the params to a list of tuples ie. List[Tuple[String, String]]. Then iterate over the params matching to the param name case and set the value.

(request.params.toList) => Future[ResponseBuilder] {
  var option1: String = null
  var option2: String = null
  for( param <- value) {
    param._1 match {
      case "option1" => option1 = param._2
      case "option2" => option2 = param._2
      case _ => { }
    }
  }
  render.body("some message").toFuture