NSURLSession多部分/表单数据发布请求出现问题 [英] Trouble with NSURLSession multipart/form-data post request
问题描述
我无法在同一发布请求中向我的服务器发送文本和图像.我认为问题与我设定边界的方式有关.
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.
我在iOS9中使用swift.
Im using swift with ios9.
我在这里遵循了说明 ios使用HTTP POST上传图像和文本 尽我所能将obj-c转换为swift
I followed instructions here ios Upload Image and Text using HTTP POST doing my best to convert the obj-c into swift
但是,当我向服务器发布请求时,每当尝试访问$ _POST ["key"]之类的发布数据时,都会收到未定义的索引错误.这是我用来设置http请求的代码,任何人都可以发现错误:
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;
推荐答案
使用多部分表单数据时,必须在主体数据中为边界加上另外两个连字符,并且还必须在末尾添加两个连字符.最终边界.因此,如果您有:
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
然后身体应该像这样:
--foo
field 1 info
--foo
field 2 info
--foo--
另请参见 multipart/form-data中的-"是什么?
这篇关于NSURLSession多部分/表单数据发布请求出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!