Created
April 29, 2016 18:18
-
-
Save developmentalmadness/917972ac97cd874625524ba89c941751 to your computer and use it in GitHub Desktop.
Play WS POST with Iteratee to Akka HTTP Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.model.HttpEntity | |
import akka.http.scaladsl.server.Directives._ | |
import akka.stream.ActorMaterializer | |
class TestWebServer(testSystem: ActorSystem){ | |
implicit val system = testSystem | |
implicit val materialize = ActorMaterializer() | |
implicit val executionContext = system.dispatcher | |
val port = 8080 | |
val route = | |
path("test") { | |
post { | |
complete(HttpEntity("POST Ok")) | |
} ~ | |
get { | |
complete(HttpEntity("GET Ok")) | |
} | |
} | |
val bindingFuture = Http().bindAndHandle(route, "localhost", port) | |
def shutdown(): Unit = bindingFuture.flatMap(_.unbind()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit val httpClient = NingWSClient() | |
implicit val execution = play.api.libs.concurrent.Execution.Implicits.defaultContext | |
val f = WS.clientUrl("http://localhost:8080/test") | |
.withMethod("POST") | |
.stream().flatMap { | |
case (resp, body) => | |
val it = Iteratee.fold[Array[Byte],Seq[Byte]](Seq.empty[Byte]) { (stream, bytes) => | |
stream ++ bytes | |
} | |
(body |>>> it).map { | |
case stream => | |
new String(stream.toArray, "UTF-8") | |
} | |
} | |
val r = Await.result(f, 1.second) | |
assert(r == "POST Ok") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment