This way or that way. When working on web service based application, we make a request to a service and expect a response. So this time i have something to share on this. Most common way to make a request to service is using POST. How would you send an image to service ? In my case i was using php based web service and client is iphone application. We generally send image by writing data to http message, where we mainly mention header and body of message. Our image data goes with the message body of http request. This is how normal http message
is composed in objective-c.

NSString *boundary = @"---------------------------14737809831466499882746641449";
        
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// end of what we've added to the header
            
NSMutableData *body = [NSMutableData data];
    
// the body of the post
UIImage *contactImage = self.originalImage;
NSData *imageData = UIImagePNGRepresentation(contactImage);
            
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now append the image
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\";filename=\"picture.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
            
// We now need to tell the receiver what content type we have
            
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// adding the body we've created to the request
[request setHTTPBody:body];

That is straight-forward and there is nothing very different with code. Now suppose you have to send some other information data along with image using same above process, so for first and few attempt i tried to write other data to message body too but i failed. I was missing something and couldn’t get it to work. So at that time i had only one solution which was working for me. The idea was to send my other data like strings and all in query string and image data via post message. So what i did was made an query string with my name balue pairs and hit this new url via post message and post message body had my image data. Ofcourse i should have been sending everything in post body but somehow it wasn’t working since i was writing all data in some wrong way as i could send only image at that time. But definitely i found the correct way and i made it right, but i’l come to that part later. For now i want to show where exactly my problem came and where i found the solution. Now my requirement was to send json object/string representation and image to server via post. Good way to do this was again writing both json and image to post message body but i wasn’t able to write both so i decided to send json string representation with query string and image data on post message body. It was definitely working. This is how i was making my json string for my array of objects and creating query string url.

NSString *jsonStr = [self.studentArray JSONRepresentation];
NSMutableString *urlString =[NSMutableString stringWithString:urlStr];
[urlString appendString:@"?student_details="];
[urlString appendString:[NSString stringWithFormat:@"%@", [jsonStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURL *url = [NSURL URLWithString:urlString];

I was hitting this url and sending image data via post, things were going fine until one big bang. If i was making json representation of 40 students inside students array and each student object has 5 members, so json representation comes out really long and big. Sending this long very long string to query string always failed. My nsurlconnection was always getting lost. Now i was left with only one option at that time, it was that i had to send both image and json on post request message body. So after many trials i finally had it working.

responseData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url 
                                                                cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                            timeoutInterval:60]; 
    
[request setHTTPMethod:@"POST"];
    
NSString *boundary = @"---------------------------14737809831466499882746641449";
        
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// end of what we've added to the header
            
NSMutableData *body = [NSMutableData data];
    
// the body of the post
UIImage *contactImage = self.originalImage;
NSData *imageData = UIImagePNGRepresentation(contactImage);
            
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now append the image
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\";filename=\"picture.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
            
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"student_detail\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/json\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", jsonString] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
// adding the body we've created to the request
[request setHTTPBody:body];
        
//[connection start];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self ];
    
[request release];

So that was it. Keep one thing in mind that “name=student_detail” matches with your request variable in web service, so whatever request variable you have in web service, just put that name here. You can write other data to post request message body too now.
Hope this works for you too.

Gracias.