我正在通过这种格式发送请求以发出带有参数的请求。但是我将如何使用此参数将图像作为多部分正文发送?
class APIManager: NSObject {
enum Router: URLRequestConvertible {
static var baseURLString = baseURLString
static let xSource = X_SOURCE_HEADER
case updateProfile([String: AnyObject])
var method: HTTPMethod {
switch self {
case .updateProfile:
return .put
}
}
var path: String {
switch self {
case .updateStockShelfUnits:
return profile_url
}
}
func asURLRequest() throws -> URLRequest {
let url = try GlobalData.gBaseURLString.asURL()
var urlRequest = URLRequest(url: url.appendingPathComponent(path))
urlRequest.httpMethod = method.rawValue
switch self {
case .updateProfile(let parameters):
urlRequest.setValue(access_token, forHTTPHeaderField: "X-User-Token")
urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
}
return urlRequest
}
}
func makeRequestToUpdateProfile(param: [String : AnyObject], img: UIImage, completion: @escaping completionHandlerWithSuccessAndErrorMessage) {
Alamofire.request(Router.updateprofile(param)) .responseJSON { response in
switch response.result {
case .success(let JSON):
print(JSON)
completion(true, "Success")
case .failure(let Error):
print("Request failed with error: \(Error)")
completion(false, Error.localizedDescription)
}
}
}
}
在这里,我将如何使用图像作为多部分主体来制作请求参数?请求只有参数的 api 运行良好。