I'm developing an iphone app and using a POST request, I already read many post from this same topic but is still not working, I will appreciate some help, here is my code:
//turning the image into a NSData object
//getting the image back out of the UIImageView
//setting the quality to 72
NSData *image_data = UIImageJPEGRepresentation(picture, 72);
// setting up the URL to post to
NSMutableString *urlString = [[NSMutableString alloc] init];
[urlString appendString:BASE_URL];
[urlString appendString:@"mobile_services/mobile_store_services/upload_wishlist_image/"];
//[urlString appendString:@"/temp_trash/temporal.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[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"];
// file
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"test.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:image_data];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Text parameter1
NSString *param1 = @"parameter text";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"texto_prueba\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn){
web_data = [NSMutableData data];
}else{
NSLog(@"doesn't exist");
}
I am using the NSURLConnectionDataDelegate protocol so, I received the data in this method:
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
//NSArray *json_data = [NSJSONSerialization JSONObjectWithData:web_data options:kNilOptions error:NULL];
NSString *returningString = [[NSString alloc] initWithData:web_data encoding:NSUTF8StringEncoding];
NSLog(@"returned string = %@", returningString);
[loadingView stopLoading];
}
my PHP side, only tell me, what it received in this way:
public function upload_wishlist_image()
{
var_dump($_REQUEST);
}
Please some help!, PHP doesn't receive any image data.