Alamofire根据输入更改多部分/表单数据编码

时间:2018-07-12 13:00:37

标签: ios swift alamofire

研究iOS项目时,我发现Alamofire会根据字段值更改Content-Transfer-Encoding字段的multipart/form-data

目标

将带有某些信息(unicode字符串)的图像更新到端点。

详细信息

我定义表单的字段值如下:

let formFields: [String: String] = [
    "key": _model
]

我上传整个表单(图像+信息),如下所示:

Alamofire.upload(
    multipartFormData: { multipartFormData in
        // 1. The form's fields
        for (key, value) in formFields {
            guard let byteString = value.data(using: String.Encoding.utf8, allowLossyConversion: false) else { continue }
            multipartFormData.append(byteString, withName: key)
        }

        // 2. The picture
        let fileName = "file.jpeg"
        multipartFormData.append(pictureData, withName: ParameterKeys.BikeImage, fileName: fileName, mimeType: "image/jpeg")
    },

    to: _url,
    method: .post,

    encodingCompletion: { encodingResult in
        switch encodingResult {

        case .success(let upload, _, _):        /* Called when the encoding succeds. */
            upload.validate(statusCode: 200..<300)

            upload.uploadProgress { progress in // main queue by default
                progressHandler(progress)
            }

            upload.response { (response) in
                responseHandler(response)
            }

        case .failure(let error):       /* Called when the encoding fails. */
            failureHandler(error)
        }
    }
)

行为0

  • _model"Standard"
  • multipartFormData.append(byteString, ...)以✅
  • 形式附加"5374616e64617264"
  • 服务器获得"Standard",而没有Content-Transfer-Encoding⁉️

行为1

  • _model"Ström Bike"
  • multipartFormData.append(byteString, ...)以✅
  • 形式附加"537472c3b66d2042696b65"
  • 服务器通过"Str=C3=B6m Bike"⁉️获得Content-Transfer-Encoding: quoted-printable

行为2

  • _model"Abräcadabra"
  • multipartFormData.append(byteString, ...)以✅
  • 形式附加"416272c3a463616461627261"
  • 服务器通过"QWJyw6RjYWRhYnJh"⁉️获得Content-Transfer-Encoding: base64

结论

在服务器上,我无法确定表单数据的格式。有没有一种方法可以指定用于表单字段的编码类型?

谢谢。

0 个答案:

没有答案