假设我们有以下要求:
curl --location --request POST 'localhost:8080/api' \
--header 'Content-Type: multipart/form-data' \
--form 'field1=value1' \
--form 'field2=value2'
下面的请求处理程序获取整个实体,但是我正在努力查看如何获取value1
和value2
。
val requestHandler: Flow[HttpRequest, HttpResponse, _] = Flow[HttpRequest].mapAsync(1) {
case HttpRequest(HttpMethods.POST, Uri.Path("/api"), _, entity, _) =>
val entityTextFuture: Future[String] = entity.toStrict(3 seconds).map(_.data.utf8String)
entityTextFuture.flatMap { text =>
Future(HttpResponse(
StatusCodes.OK,
entity = text
))
}
}
重要:我必须使用Akka HTTP低级服务器API,所以我不能使用路由。
非常感谢您的时间和提前的帮助!
如果只需要表单数据中的字符串值,则只需解组为StrictForm
,然后将每个字段值解组为字符串。
这是概念证明Ammonite脚本,它使用value1 & value2
响应您的curl请求:
import $ivy.`com.typesafe.akka::akka-actor:2.6.3`
import $ivy.`com.typesafe.akka::akka-stream:2.6.3`
import $ivy.`com.typesafe.akka::akka-http:10.1.11`
import scala.concurrent.Future
import akka.actor.ActorSystem
import akka.stream.scaladsl.Flow
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.http.scaladsl.common.StrictForm
implicit val system = ActorSystem()
implicit val ec = system.dispatcher
val requestHandler: Flow[HttpRequest, HttpResponse, _] = Flow[HttpRequest].mapAsync(1) {
case HttpRequest(HttpMethods.POST, Uri.Path("/api"), _, entity, _) =>
for {
strictForm <- Unmarshal(entity).to[StrictForm]
fieldsSeq <- Future.traverse(strictForm.fields) {
case (n, v) => Unmarshal(v).to[String].map(n -> _)
}
fields = fieldsSeq.toMap
response = fields("field1") + " & " + fields("field2")
} yield HttpResponse(StatusCodes.OK, entity = response)
}
Http().bindAndHandle(requestHandler, "localhost", 8080)