研究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)
}
}
)
_model
是"Standard"
✅multipartFormData.append(byteString, ...)
以✅"5374616e64617264"
"Standard"
,而没有Content-Transfer-Encoding
⁉️_model
是"Ström Bike"
✅multipartFormData.append(byteString, ...)
以✅"537472c3b66d2042696b65"
"Str=C3=B6m Bike"
⁉️获得Content-Transfer-Encoding: quoted-printable
_model
是"Abräcadabra"
✅multipartFormData.append(byteString, ...)
以✅"416272c3a463616461627261"
"QWJyw6RjYWRhYnJh"
⁉️获得Content-Transfer-Encoding: base64
在服务器上,我无法确定表单数据的格式。有没有一种方法可以指定用于表单字段的编码类型?
谢谢。