I want to send file of image on server when I edit choose image button, but I don't know why my image profile didn't update on sever , here is my console debugger area when app run :
and also I have this message from server that "please upload an image" this response should change to "your profile photo updated" and also this my code :
func imageInput(asset : String) {
let semaphore = DispatchSemaphore (value: 0)
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/" + asset
let parameters = [
[
"key": "image",
"src": "\(path)",
"type": "file"
]] as [[String : Any]]
let boundary = "Boundary-\(UUID().uuidString)"
var body = ""
var error: Error? = nil
for param in parameters {
if param["disabled"] == nil {
let paramName = param["key"]!
print("params image key is \(paramName)")
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if param["contentType"] != nil {
body += "\r\nContent-Type: \(param["contentType"] as! String)"
}
let paramType = param["type"] as! String
if paramType == "text" {
let paramValue = param["value"] as! String
print("value of param is \(paramValue)")
body += "\r\n\r\n\(paramValue)\r\n"
} else {
if path != nil {
body += "; filename=\"\(path)\"\r\n"
+ "Content-Type: \"content-type header\"\r\n\r\n \(path)\r\n"
print("this is going to true")
}
}
}
}
body += "--\(boundary)--\r\n";
let postData = body.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.offernews.co/api/user")!,timeoutInterval: Double.infinity)
request.addValue("baerar \(profileKeychain["token"]!)", forHTTPHeaderField: "Authorization")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpMethod = "PATCH"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data
else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()
}
and also this is my another question like this that every code that you want on this link Photo did not update when I patch it to json server swift thanks guys