I am new to akka http. I have created a program to make post request using http as follow -
object MainController {
def main(args: Array[String]) {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
val serverSource = Http().bind(interface = "localhost", port = 9000)
val requestHandler: HttpRequest => HttpResponse = {
case HttpRequest(GET, Uri.Path("/welcome"), _, _, _) =>
HttpResponse(entity = HttpEntity(
ContentTypes.`text/html(UTF-8)`,
"<html><body>Welcome to API Application</body></html>"))
case HttpRequest(POST, Uri.Path("/parseData"), _, entity: HttpEntity, _) =>
// Here Need to read request body which is in json format
println("1 " + new String(entity.getDataBytes()))
println("2 " + entity.getDataBytes())
// here need to do some calculations and again construct array of json response and send as HttpResponse
HttpResponse(entity = "PONG!")
case r: HttpRequest =>
r.discardEntityBytes() // important to drain incoming HTTP Entity stream
HttpResponse(404, entity = "Unknown resource!")
}
val bindingFuture = Http().bindAndHandleSync(requestHandler, "localhost", 9000)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
}
As i have mentioned in above code inside "Post" request that I need to read the request body data which is array of json and do some calculations and finally send the processed array of json to HTTPResponse. Tried even High level API as well but it is again getting stuck in Marshalling. can anyone please explain or help me in this.
I have tried another approach as follows -
object MainController {
// needed to run the route
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
final case class hexRecord(hexstring: String)
final case class DeviceData(hexData: List[hexRecord])
// formats for unmarshalling and marshalling
implicit val contentFormat = jsonFormat1(hexRecord)
implicit val dataFormat = jsonFormat1(DeviceData)
def main(args: Array[String]) {
implicit val formats = org.json4s.DefaultFormats
val requestBody = List.empty[Map[String, Any]]
val route: Route =
concat(
get {
path("welcome"){
complete("Welcome to Parsing Application")}
},
post {
path("parseDeviceData") {
entity(as[DeviceData]) { data => {
val result = data.hexData.map(row => {
val parseData = ParserManager(Hex.decodeHex(row.hexstring.replaceAll("\\s", "").toCharArray))
val jsonString = Serialization.writePretty(parseData)
jsonString
}).toArray
complete(result)
}
}
}
}
)
val bindingFuture = Http().bindAndHandle(route, "localhost", 9000)
println(s"Server online at http://localhost:9000/")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
}
This result is fine here but I am getting escape characters in output -
[
" {\n \"totalsize\" : 128,\n \"devicetypeuno\" : \"2\"} ",
" {\n \"totalsize\" : 128,\n \"devicetypeuno\" : \"2\"} "
]