pbnsilva
pbnsilva

Reputation: 363

Spray multiple post parameters

How to handle more than one parameter in the post body. To handle one I do:

post {
            respondWithMediaType(`application/json`) {
              entity(as[String]) { text =>
                complete(extract(text).toJson.compactPrint)
              }
            }
          }

Now I need to get a seconds double parameter.

Any help?

Thanks

Upvotes: 0

Views: 836

Answers (1)

grotrianster
grotrianster

Reputation: 2468

Define case class with your desired two fields

case class MyClass(first: String, second: Double)

create Json format for MyClass

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val formatMyClass = jsonFormat2(MyClass)
}

Spray will deserialize json to MyClass

post {
   respondWithMediaType(`application/json`) {
              entity(as[MyClass]) { myClass =>
                complete(extract(text).toJson.compactPrint)
              }
   }
}

Upvotes: 4

Related Questions