I want to send the image as a parameter along with my request. I used the code below to call my POST request, but I do not know how to add an image to the body.
I get the image through the image picker as follows:
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
My request is formed as below
var request = URLRequest(url: URL(string: "")!)
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 {
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 saw this through multipart / form-data, but could not implement it myself. I do not want to encode it in base 64 format. Please help me with this.
source
share