具有特定 JSON 要求的多部分表单数据 POST

Multipart Form Data POST with spesific JSON Requirement

我是 Alamofire 的新手,现在使用 Alamofire 5。我想创建一个包含多部分表单数据的 POST 请求,但对 JSON 正文有特定要求。这是:

"item": [
    {
        "name": "Upload image",
        "request": {
            "method": "POST",
            "header": [],
            "body": {
                "mode": "formdata",
                "formdata": [
                    {
                        "key": "files[]",
                        "type": "file",
                        "src": []
                    },
                    {
                        "key": "mode",
                        "value": "public",
                        "type": "text"
                    }
                ]
            },
            "url": {
                "raw": "https://jsonplaceholder.typicode.com/api/image/upload",
                "protocol": "https",
                "host": [
                    "jsonplaceholder",
                    "typicode",
                    "com"
                ],
                "path": [
                    "api",
                    "image",
                    "upload"
                ]
            }
        },
        "response": []
    },
]

任何人都可以帮助我如何 post 使用多部分表单数据的数据?请帮忙。 (如果 POST 请求使用 URLSession 就可以了)

无论我从您的问题和评论中了解到什么,我已经根据您之前的问题创建了一个方法。

func postImage(images: [UIImage],imgName : [String]) {
    var arrFormData = [[String:Any]]()
    var imgDataArray: [Data] = []

    for image in images {
        guard let imgData = image.jpegData(compressionQuality: 0.50) else { return }
        
        imgDataArray.append(imgData)
    }
    let param1: [String: Any] = [
            "key":"files[]",
            "type": "file",
            "src": imgName
        ]
    let param2: [String: Any] = [
        "key": "mode",
        "value": "public",
        "type": "text"
        ]
    var arrParam = [[String:Any]]()
    arrParam.append(param1)
    arrParam.append(param2)
    arrFormData.append(contentsOf: arrParam)
    var param : [String:Any] = [:]
    if let theJSONData = try? JSONSerialization.data(
        withJSONObject: arrFormData,
        options: []) {
        let theJSONText = String(data: theJSONData,
                                   encoding: .ascii)
        print("JSON string = \(theJSONText!)")
        param = ["formData" : theJSONText ?? ""]
    }
    print(param)
    Alamofire.upload(multipartFormData: {
        multipartFormData in
         for i in 0..<images.count{
            if let imageData = images[i].jpegData(compressionQuality: 0.6) {
                multipartFormData.append(imageData, withName: "file", fileName: "name.png", mimeType: "image/png")
            }
        }

        for (key, value) in param {
            multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
        }
    }, usingThreshold: 10 * 1024 * 1024,to: apiurl, method: .post, headers: headers, encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON {
                response in
                print(response.result)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    })
}