Skip to content

Instantly share code, notes, and snippets.

@dcaoyuan
Created August 26, 2016 12:23
Show Gist options
  • Save dcaoyuan/f2f70bed35c647a4d9a43dfdbcb6dbb8 to your computer and use it in GitHub Desktop.
Save dcaoyuan/f2f70bed35c647a4d9a43dfdbcb6dbb8 to your computer and use it in GitHub Desktop.
akka-http-client-example
val uri = "http://www.yahoo.com"
val reqEntity = Array[Byte]()
val respEntity = for {
request <- Marshal(reqEntity).to[RequestEntity]
response <- Http().singleRequest(HttpRequest(method = HttpMethods.POST, uri = uri, entity = request))
entity <- Unmarshal(response.entity).to[ByteString]
} yield entity
val payload = respEntity.andThen {
case Success(entity) =>
s"""{"content": "${entity.utf8String}"}"""
case Failure(ex) =>
s"""{"error": "${ex.getMessage}"}"""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment