I want to send an image as a parameter along with my request.
I have used the code below to call my POST request but I don't know how to append image to the body.
I am getting the image through image picker as follows:
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
My request is formed as below
var request = URLRequest(url: URL(string: "")!) // link removed
request.httpMethod = "POST"
let postString = "user_id=(userId)&image=(image)"
request.httpBody = postString.data(using:.utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
return
}
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? AnyObject
if let parseJSON = json {
print("resp :(parseJSON)")
}
} catch let error as NSError {
print("error : (error)")
}
}
task.resume()
I am new to Swift. I have seen this through multipart/form-data but unable to implement it myself. I do not want to encode it in base 64 format. Please help me in this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…