Skip to content

Instantly share code, notes, and snippets.

@pablete
Forked from dcaoyuan/akka-http-client.scala
Created September 8, 2017 07:26
Show Gist options
  • Save pablete/7d29f578709fbab3b60a98e1c9dccfb2 to your computer and use it in GitHub Desktop.
Save pablete/7d29f578709fbab3b60a98e1c9dccfb2 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