HttpRequest and HttpResponse

All 3 Akka HTTP Client API levels use the same basic model of HttpRequestHttpRequest and HttpResponseHttpResponse.

Creating requests

You can create simple GET requests:

Scala
HttpRequest(uri = "https://akka.io")
Java
HttpRequest.create("https://akka.io");

Or more complicated ones, like this POST:

Scala
HttpRequest(
  method = HttpMethods.POST,
  uri = "https://userservice.example/users",
  entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, "data")
)
Java
HttpRequest.POST("https://userservice.example/users")
  .withEntity(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, "data"));

See the API documentation of HttpRequestHttpRequest for more information on how to customize your requests.

Processing responses

When you receive a response, you can use the Marshalling API to convert the response entity into an object:

Scala
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._

case class Pet(name: String)
implicit val petFormat = jsonFormat1(Pet)

val pet: Future[Pet] = Unmarshal(response).to[Pet]
Java
CompletionStage<Pet> pet = Jackson.unmarshaller(Pet.class).unmarshal(response.entity(), materializer);
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.