ios Upload Image and Text using HTTP POST ios Upload Image and Text using HTTP POST ios ios

ios Upload Image and Text using HTTP POST


Here's code from my app to post an image to our web server:

// Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];[_params setObject:[NSString stringWithString:@"1.0"] forKey:[NSString stringWithString:@"ver"]];[_params setObject:[NSString stringWithString:@"en"] forKey:[NSString stringWithString:@"lan"]];[_params setObject:[NSString stringWithFormat:@"%d", userId] forKey:[NSString stringWithString:@"userId"]];[_params setObject:[NSString stringWithFormat:@"%@",title] forKey:[NSString stringWithString:@"title"]];// the boundary string : a random string, that will not repeat in post data, to separate post data fields.NSString *BoundaryConstant = [NSString stringWithString:@"----------V2ymHFg03ehbqgZCaKO6jy"];// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ NSString* FileParamConstant = [NSString stringWithString:@"file"];// the server url to which the image (or the media) is uploaded. Use your server url hereNSURL* requestURL = [NSURL URLWithString:@""]; // create requestNSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];                                    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];[request setHTTPShouldHandleCookies:NO];[request setTimeoutInterval:30];[request setHTTPMethod:@"POST"];// set Content-Type in HTTP headerNSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];[request setValue:contentType forHTTPHeaderField: @"Content-Type"];// post bodyNSMutableData *body = [NSMutableData data];// add params (all params are strings)for (NSString *param in _params) {    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];}// add image dataNSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);if (imageData) {    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];    [body appendData:imageData];    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];}[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];// setting the body of the post to the reqeust[request setHTTPBody:body];// set the content-lengthNSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long) [body length]];[request setValue:postLength forHTTPHeaderField:@"Content-Length"];// set URL[request setURL:requestURL];


XJones' answer worked like charm.

But he didn't mentioned/declared variables _params, BoundaryConstant and requestURL. So, i thought of posting that part as an add-on to his post, so that it may help others in future.

// Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];[_params setObject:[NSString stringWithString:@"1.0"] forKey:[NSString stringWithString:@"ver"]];[_params setObject:[NSString stringWithString:@"en"] forKey:[NSString stringWithString:@"lan"]];[_params setObject:[NSString stringWithFormat:@"%d", userId] forKey:[NSString stringWithString:@"userId"]];[_params setObject:[NSString stringWithFormat:@"%@",title] forKey:[NSString stringWithString:@"title"]];// the boundary string : a random string, that will not repeat in post data, to separate post data fields.NSString *BoundaryConstant = [NSString stringWithString:@"----------V2ymHFg03ehbqgZCaKO6jy"];// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ NSString* FileParamConstant = [NSString stringWithString:@"file"];// the server url to which the image (or the media) is uploaded. Use your server url hereNSURL* requestURL = [NSURL URLWithString:@""]; 

As i mentioned earler, this is not an answer by itself, just an addon to XJones' post.


For http post image and username and password sending through post method

 NSString *str=[NSString stringWithFormat:@"%@registration.php",appdel.baseUrl]; NSString *urlString = [NSString stringWithFormat:@"%@",str]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSMutableData *body = [NSMutableData data]; NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imgData]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; //  parameter username [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[userName.text dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; //  parameter token [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[eMail.text dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // parameter method [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"pass\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[passWord.text dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; //parameter method NSLog(@"%@",countryCode); [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"country\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[countryCode dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // close form [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // setting the body of the post to the reqeust [request setHTTPBody:body]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil]; Nslog(@"%@",dict); //