HTTP表示表单数据请求

HTTP表示表单数据请求

本文介绍了如何使用Akka HTTP表示表单数据请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为使用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 实体类型

 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