我想用Alamofire发出发布请求,并使用Content-Type: multipart/form-data
接受请求并接受有效载荷文件,正如您从代码打击中看到的那样,我尝试发出请求,但是我收到了500个错误代码,我确实做了邮递员中的相同请求可以正常工作,注意发出请求后,我不希望响应返回为空
let body: [String:String] = [
"id":"101",
"message":test,
"type":"test"
]
let payload = [
"payload":body
]
let headers = [ "Content-Type" : "multipart/form-data"]
Alamofire.request("URL", method: .post, parameters:payload, encoding: JSONEncoding.default, headers: headers).responseObject { (response: DataResponse<ObjectEntity>) in
guard (response.response?.statusCode == 200 || response.response?.statusCode == 204) else {
if response.response != nil {
self.showAPILogs(fullURL: self.getFullURL(methodName: methodName), response: response.response, statusCode: response.response!.statusCode)
}
return
}
self.showAPILogs(fullURL: self.getFullURL(methodName: methodName), response: response.response, statusCode: response.response!.statusCode)
}
答案 0 :(得分:0)
尝试
let body: [String: String] = [
"id": "101",
"message": test,
"type": "test"
]
Alamofire.upload(multipartFormData: { MultipartFormData in
for (key, value) in body {
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, usingThreshold: UInt64.init(),
to: "URL",
method: .post,
headers: ["Authorization": "Your access token"], // As per the web service requirement
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
}
case .failure(let error):
print(error)
}
})
看看是否可行。