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)
)
)
}
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"))
)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.