我正在尝试使用Swift将视频上传到我的Rails API后端。要上传,我使用Alamofire。唯一的问题是rails api期望这样:{"video":{"clip": (form data)}}
。我在存储多部分表单数据时遇到的问题是,似乎我无法将其存储在第二个字典中。我已经为视频尝试了params,但似乎无法正常工作。这是我的一些代码(如果有帮助的话):
Alamofire请求
AF.upload(
multipartFormData: { multipartFormData in
for (_, _) in params {
multipartFormData.append(self.videoURL, withName: "clip" , fileName: "clip.mp4", mimeType: "video/mp4")
}
multipartFormData.append("\(Id)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"Id")
},
to: "http://10.0.0.2:3000/api/v1/videouploads.json", method: .post, headers: headers)
.response { resp in
print(resp)
}
服务器期望的内容(精美打印)
{
"video": {
"clip": (multipartformdata)
}
"id": (user id)
}
答案 0 :(得分:0)
尝试一下:
multipartFormData.append(
self.videoURL,
withName: "video[clip]", // this is location in form' struct
fileName: "clip.mp4",
mimeType: "video/mp4"
)