2
votes

I have this post request and have two issues.

1.is with the headers value. The documentation says it takes in a Seq[HttpHeader] and I pass in a Seq[RawHeader] which extends HttpHeader but it says it's a type mismatch. Why?

2.I pass in the data I want to post, but the HttpEntity.Default() takes in a Source[Bytestring]. How do I convert my data to Source[Bytestring]

def post(data: String): Unit = {
    val headers = Seq(RawHeader("X-Access-Token", "access token"))
    val responseFuture: Future[HttpResponse] =
      Http(system).singleRequest(
        HttpRequest(
        HttpMethods.POST,
        "https://beta-legacy-api.ojointernal.com/centaur/user",
        headers,
        entity = HttpEntity.Default(data)
        )
      )
  }
1

1 Answers

3
votes

I pass in a Seq[RawHeader] which extends HttpHeader but it says it's a type mismatch. Why?

Because Seq[A] is invariant in A.

How do I convert my data to Source[Bytestring]

You don't have to. You can use use the HttpEntity apply method which takes an Array[Byte], and use withHeaders:

import akka.http.scaladsl.model._

def post(data: String): Unit = {
  val responseFuture: Future[HttpResponse] =
    Http(system).singleRequest(
      HttpRequest(
        HttpMethods.POST,
        "https://beta-legacy-api.ojointernal.com/centaur/user",
        entity = HttpEntity(ContentTypes.`application/json`, data.getBytes())
        ).withHeaders(RawHeader("X-Access-Token", "access token"))
    )
}