Trouble with NSURLSession multipart/form-data post request

廉价感情. 提交于 2019-12-10 11:36:47

问题


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;

回答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?



来源:https://stackoverflow.com/questions/38028533/trouble-with-nsurlsession-multipart-form-data-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!