如何使用 Akka HTTP 表示表单数据请求?

问题描述:

我想创建一个表单数据 http 请求到 Facebook API 使用 Akka HTTP.在 curl 中,请求示例如下所示:

I want to create a form-data http request to Facebook API using Akka HTTP. In curl, the request example looks like:

curl 
 -X POST 
 "https://graph-video.facebook.com/v2.3/1533641336884006/videos"  
 -F "access_token=XXXXXXX" 
 -F "upload_phase=transfer" 
 -F "start_offset=0" 
 -F "upload_session_id=1564747013773438" 
 -F "video_file_chunk=@chunk1.mp4"

因此,我为请求有效负载表示创建了以下模型:

So I created a following model for a request payload representation:

case class FBSingleChunkUpload(accessToken: String, 
    sessionId: String,
    from: Long, 
    to: Long, 
    file: File) //file is always ~1Mb

然后我将使用:

Http().cachedHostConnectionPoolHttps[String]("graph-video.facebook.com")

但我不知道如何将 FBSingleChunkUpload 转换为正确的 HttpRequest :(

But I don't know how to convert FBSingleChunkUpload to the correct HttpRequest :(

你能给我一个提示,如何使用 Akka HTTP 来表示这种类型的请求吗?

Can you give me a hint, how to represent a such type of requests using Akka HTTP?

有一个 FormData Entity 类型

 val fBSingleChunkUpload = ???
 HttpRequest(method = HttpMethods.POST,
              entity = FormData(("accessToken", fBSingleChunkUpload.accessToken), ...).toEntity)

此外,对于文件,您可以检查它是否适用于 Multipart.FormData

Additionally for the file you could check if it works with Multipart.FormData

 val fileFormPart = Multipart.FormData
    .BodyPart("name", HttpEntity(MediaTypes.`application/octet-stream`, file.length(), FileIO.fromPath(file.toPath)))

  val otherPart = Multipart.FormData.BodyPart.Strict("accessToken", "the token")

  val entity =
    Multipart.FormData(otherPart, fileFormPart).toEntity