I'm trying to send a picture which will be taken from camera or photo gallery in my app. I searched the whole site and found some code which helped. I then customized it a bit.
let imageData = UIImageJPEGRepresentation(myImageView.image!, 1)
if imageData != nil{
let request = NSMutableURLRequest(url: NSURL(string:"http://app.avatejaratsaba1.com/api/Ticket/SendTicket")! as URL)
var session = URLSession.shared
request.httpMethod = "POST"
let boundary = NSString(format: "---------------------------14737809831466499882746641449")
let contentType = NSString(format: "multipart/form-data; boundary=%@",boundary)
// println("Content Type \(contentType)")
request.addValue(contentType as String, forHTTPHeaderField: "Content-Type")
let body = NSMutableData()
// Title
body.append(NSString(format: "\r\n--%@\r\n",boundary).data(using: String.Encoding.utf8.rawValue)!)
body.append(NSString(format:"Content-Disposition: form-data; name=\"title\"\r\n\r\n").data(using: String.Encoding.utf8.rawValue)!)
body.append("Hello World".data(using: String.Encoding.utf8, allowLossyConversion: true)!)
// Image
body.append(NSString(format: "\r\n--%@\r\n", boundary).data(using: String.Encoding.utf8.rawValue)!)
body.append(NSString(format:"Content-Disposition: form-data; name=\"profile_img\"; filename=\"img.jpg\"\\r\n").data(using: String.Encoding.utf8.rawValue)!)
body.append(NSString(format: "Content-Type: application/octet-stream\r\n\r\n").data(using: String.Encoding.utf8.rawValue)!)
body.append(imageData!)
body.append(NSString(format: "\r\n--%@\r\n", boundary).data(using: String.Encoding.utf8.rawValue)!)
request.httpBody = body as Data
do{
let returnData = try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: nil)
let returnString = NSString(data: returnData, encoding: String.Encoding.utf8.rawValue)
print("returnString \(returnString)")
}
catch{print("gg")
}
}
Right now as I run this function, the server returns :
502 - Web server received an invalid response while acting as a gateway or proxy server.
There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.
This is because I'm not send 3 parameters. I wanted to know how should I add parameters (2 String, 1 Integer - in total: 2 String, 1 integer , 1 Image)
Any suggestion on how to add parameters?