1

Im having some trouble with sending text and images in the same post request to my server. I think the problem has to do with the way i set my boundary.

Im using swift with ios9.

I followed instructions here ios Upload Image and Text using HTTP POST doing my best to convert the obj-c into swift

however when i post a request to my server, whenever i try to access the post data such as $_POST["key"] i get an undefined index error. Here is the code i use to setup the http request, can anyone spot the error:

 func sendRegisterRequest(params:Dictionary<String, String>, withImage image: UIImage) {

    // https://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post
    let url = NSURL(string: "MY_URL");
    let request = NSMutableURLRequest(URL: url!)
    // the boundary string : a random string, that will not repeat in post data, to separate post data fields.
    let boundaryConstant = "----------V2ymHFg03ehbqgZCaKO6jy--";

    // string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
    let fileParamConstant = "file";

    request.HTTPMethod = "POST"
    request.setValue("multipart/form-data; boundary=\(boundaryConstant)", forHTTPHeaderField: "Content-Type")


    // post body
    let body = NSMutableData();



    // add params (all params are strings)
    for (key, value) in params {
        body.appendData("\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!);
        body.appendData("Content-Disposition: form-data; name=\"\(key.stringByAddingPercentEncodingWithAllowedCharacters(.symbolCharacterSet())!)\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!);
        body.appendData("\(value.stringByAddingPercentEncodingWithAllowedCharacters(.symbolCharacterSet())!)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!);
    }

    //print(body);
    // add image data
    let imageData = UIImageJPEGRepresentation(image, 1.0);

    body.appendData("\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!);
    body.appendData("Content-Disposition: form-data; name=\"\(fileParamConstant)\"; filename=\"image\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!);
    body.appendData("Content-Type: image/jpeg\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!);
    body.appendData(imageData!);
    body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!);
    body.appendData("\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!);

    request.HTTPBody = body;
Community
  • 1
  • 1
Tyler Durden
  • 575
  • 7
  • 23
  • you can check this answer http://stackoverflow.com/questions/26162616/upload-image-with-parameters-in-swift/26163136#26163136 – Shivani Gor Jun 25 '16 at 13:26

1 Answers1

1

When you use multipart form data, you must prefix the boundary with an additional two hyphens within the body data, and you must also add two hyphens at the end of the final boundary. So if you have:

boundary=foo

then the body should look like this:

--foo
field 1 info
--foo
field 2 info
--foo--

See also What is the '-' in multipart/form-data?

Community
  • 1
  • 1
dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • Thanks, I corrected this and now it works correctly. I also had to remove `stringByAddingPercentEncodingWithAllowedCharacters(.symbolCharacterSet())`. – Tyler Durden Jul 01 '16 at 20:07