views:

96

answers:

1

hi all!

I'm using the following lines of code to make an http post call

NSData *imageData = UIImageJPEGRepresentation(image, 0.9);
NSString *urlString = @"http://169.254.225.91:8888/upload.php";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

how do I add extra string parameters in the body?

A: 

Found a solution:

NSMutableData *body = [NSMutableData data];

//title caption  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"caption\"\r\n\r\n%@", caption.text] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
//inputname  
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"inputname\"\r\n\r\n%@", name.text] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
//emailadress  
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"emailadress\"\r\n\r\n%@", email.text] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
//frameID  
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"frameID\"\r\n\r\n%@", @"1"] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  

//Image  
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",[caption text]] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[NSData dataWithData:imageData]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
Thomas Joos