表示表单数据请求

表示表单数据请求

本文介绍了如何使用 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 "[email protected]"

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

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

这篇关于如何使用 Akka HTTP 表示表单数据请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:14