Hi, I need to upload image to custom server. I have code like this:
NSString* boundary = @"blablablablabla";
NSString* boundaryString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
NSString* boundaryStringFinal = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
NSMutableData* postData = [NSMutableData dataWithCapacity:[imageData length] + 1024];
NSString* s = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"s\"\r\n\r\n%@\r\n", @"addimage"];
NSString* ad = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"ad\"\r\n\r\n%@\r\n", adIdx];
NSString* u = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"u\"\r\n\r\n%@\r\n", uid];
[postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[s dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[ad dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[u dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\";\r\nfilename=\"image.jpg\"\r\nContent-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[postData appendData:[boundaryStringFinal dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:a.b.c/d.php"]];
[request setHTTPMethod:@"POST"];
NSString* dataLength = [NSString stringWithFormat:@"%d", [postData length]];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
[request addValue:@"a.b.c" forHTTPHeaderField:@"Host"];
[request addValue:dataLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:self];
It looks good but in response for this request I have line like this: "Missing paramter u". Can somebody tell me where is the problem?