Im trying to upload a file to the backend. edit
I'm at a stand still with this code. I feel like I'm doing this wrong. Its returning (Status Code: 400, Headers ) Which makes me believe that the error is in the headers but I may be totally wrong because I see people using .upload() instead of .request. I've made 2 functions based on these two methods, neither one works. .request is error 400, upload() is error 500. I've tried the access token in L5 Swagger and it works. The only parameter is a file which I assume is data. Here are the methods
@discardableResult
public func postUploadSeekerAvatar(_ image: UIImage, result: @escaping (Error?) -> Void) -> URLSessionTask? {
let imageData: Data = UIImagePNGRepresentation(image)!
let params: [String: Any] = ["data": imageData]
var headers = authHeader
headers?["Content-Type"] = "application/json"
return Alamofire.request(endpointURL("users/seeker/avatar"), method: .post, parameters: params, headers: headers)
.responseData(completionHandler: { (response: DataResponse<Data>) in
guard response.result.error == nil else {
return result(response.result.error)
}
return result(response.result.error)
}).task
}
@discardableResult
public func postSeekerAvatar(_ image: UIImage, result: @escaping (Error?) -> Void) -> URLSessionTask? {
let data = UIImagePNGRepresentation(image)!
var headers = authHeader
headers?["Content-Type"] = "application/json"
return Alamofire.upload(data, to: endpointURL("users/seeker/avatar"), method: .post, headers: headers)
.responseData(completionHandler: { (response: DataResponse<Data>) in
guard response.result.error == nil else {
return result(response.result.error)
}
return result(response.result.error)
}).task
}
Can you sirs and madams please help a newbie? Thank you!