I’m trying to create a Post request path using the Akka HTTP Server and the Scala programming language.
After I successfully make and receive the post request data, I would like to access the value of the country
field inside the device
parameter.
What is the best way to access this parameter?
I am using SprayJSON as the marshaller.
The current code gives the error Found: Option[Option[String]] Required: String
.
Again, the request is processed successfully, I just can’t figure out how to access the value of the country
field inside the device
parameter.
The code is as below.
The case classes:
case class Geo(country: Option[String])
case class Device(id: String, geo: Option[Geo])
case class dataRequest(id: String, device: Option[Device])
The Post Request path:
Path(“submit”) {
Post {
Entity(as[dataRequest]) { data_request_params =>
data_request_params.device.map(dev => dev.geo.map(geo => {
val geoDevice = geo.country
}))
}
}
}
The request body:
{
“id”: “fghq1I123”,
“device”: {
“id”: “0004a672f”,
“geo”: {
“Country”: “AU”
}
}
}