0

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?

Api

Mattie
  • 2,868
  • 2
  • 25
  • 40
Am1rFT
  • 227
  • 5
  • 18
  • Seems you have found a very, very old and bad code. `NSURLConnection` is deprecated and using `sendSynchronousRequest` is strongly discouraged, which can be a reason for your app to be rejected. And with replacing all the `NS`-stuffs your code can be more simple. And the important thing is your way of creating `multipart/form-data` is a little bit broken. You should better search with "swift urlsession multipart/form-data" and find a right code first, and then adapt the code to your server. – OOPer Jul 12 '18 at 21:54
  • One more, are you sure your server really accepts `multipart/form-data`? Some servers do not use `multipart/form-data` even for image uploading. – OOPer Jul 12 '18 at 22:02
  • @OOPer , Yes I'm sure – Am1rFT Jul 12 '18 at 22:15
  • Ok, then please read my first comment carefully. – OOPer Jul 12 '18 at 22:16
  • @OOPer||| before i find this code , i was not able to send any request to server! but now at least i'm getting error :| , – Am1rFT Jul 12 '18 at 22:22
  • I repeat, that's bad code to start with. Find the right one. – OOPer Jul 12 '18 at 22:36

1 Answers1

1

For better or worse, I've had to write a number of multipart mime encoders for iOS - though all in Objective-C. You can find all of the gory details about the format in the RFC. I really can't stand the format, but it's challenging to send binary data to HTTP servers without it.

It looks to me like you're almost there. You just need to add some more multipart parts to your message.

You can see some examples of what that looks like in this question.

Mattie
  • 2,868
  • 2
  • 25
  • 40