Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic way to create a basic HTTP Post request with Akka HTTP

I'm trying to figure out how to create a basic HTTP POST request with the Akka HTTP library. This is what I came up with:

val formData = Await.result(Marshal(FormData(combinedParams)).to[RequestEntity], Duration.Inf)
val r = HttpRequest(POST, url, headers, formData)

The thing is that it seems a bit non-idiomatic to me. Are there other ways to create a HttpEntity from FormData? Especially the fact that I have to use Await or return a Future even though the data is readily available seems overly complex for such a simple task.

like image 247
Frank Versnel Avatar asked Sep 24 '15 09:09

Frank Versnel


1 Answers

You can use Marshal in a for comprehension with other Futures, such as the ones you need to send the request and unmarshall the response:

val content = for {
        request <- Marshal(formData).to[RequestEntity]
        response <- Http().singleRequest(HttpRequest(method = HttpMethods.POST, uri = s"http://example.com/test", entity = request))
        entity <- Unmarshal(response.entity).to[String]
      } yield entity
like image 117
mattinbits Avatar answered Sep 18 '22 18:09

mattinbits