gpt4 book ai didi

swift - 如何使用 alamofire 的多部分表单数据发送嵌套数组参数

转载 作者:可可西里 更新时间:2023-11-01 02:16:11 25 4
gpt4 key购买 nike

如何将这个参数传给multipart

    let dictionary = [
"user" :
[
"email" : "\(email!)",
"token" : "\(loginToken!)"
],
"photo_data" :[
"name" : "Toko Tokoan1",
"avatar_photo" : photo,
"background_photo" : photo,
"phone" : "0222222222",
"addresses" :[[
"address" : "Jalan Kita",
"provinceid" : 13,
"cityid" : 185,
"postal" : "45512"
]],
"banks" :[[
"bank_name" : "PT Bank BCA",
"account_number" : "292993122",
"account_name" : "Tukiyeum"
]]

]

]

我试过下面的代码,但我无法将值(在 NSDic 中)编码为 utf 8

        for (key, value) in current_user {
if key == "avatar_photo" || key == "background_photo"{
multipartFormData.appendBodyPart(fileURL: value.data(using: String.Encoding.utf8)!, name: key) // value error because its NSDic
}else{
multipartFormData.appendBodyPart(data: value.data(using: String.Encoding.utf8)!, name: value) // value error because its NSDic
}

}

value 无法使用追加正文部分,因为它是 NSDictionary 而不是字符串。将该参数放入 multipartformdata 的正确方法是什么?

最佳答案

允许嵌套多部分。

The use of a Content-Type of multipart in a body part within another multipart entity is explicitly allowed. In such cases, for obvious reasons, care must be taken to ensure that each nested multipart entity must use a different boundary delimiter.

RFC 1341

因此您必须执行与在外循环中所做的相同的操作:简单地遍历生成键值对的字典的内容。显然,您必须设置不同的部分分隔符,以便客户端可以区分嵌套部分更改和顶级部分更改。

也许将整个结构作为 application/json 发送会更容易。

关于swift - 如何使用 alamofire 的多部分表单数据发送嵌套数组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38423417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com