troubleshooting Question

Can anyone find the error in this POST NSMutableURLRequest to a PHP script?

Avatar of Cuahutleco
Cuahutleco asked on
CPHPSwift Programming
2 Comments1 Solution920 ViewsLast Modified:
I try to send data to a php script  that stores the information.

And I'm trying to send a syncronousRequest with Objc code from iPhone using a POST NSMutableURLRequest.

But the answer always returns null values:

> [DEBUG]... response from request:
> There was an error, all field not
> existe, please try again!<br />format
> = <br />category = <br />title = <br />description = <br />photo =<br
> />latitude = <br />longitude =

What am I doing wrong? because what I see is that no field is received.

PHP SCRIPT:
 
 
mb_internal_encoding("UTF-8");
        
        if(isset($_POST['format']) && isset($_POST['category']) && isset($_POST['title']) && isset($_POST['description']) && isset($_FILES['photo']) {
        	save($_POST['category'], $_POST['title'], $_POST['description'], $_FILES['photo']); 
        } else {
        	echo "There was an error, a field does not exist, please try again!<br />";
        	echo "format = " . $_POST['format'] . "<br />";
        	echo "category = " . $_POST['category'] . "<br />";
        	echo "title = " . $_POST['title'] . "<br />";
        	echo "description = " . $_POST['description'] . "<br />";
        	echo "photo =" . $_FILES['photo'] . ;
        }
    ...
    ...
 
 
OBJ-C Client code:
 
 
 //creating the url request:
    	NSURL *cgiUrl = [NSURL URLWithString:@"http://www.hhh.com/uploading.php"];
    	NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:cgiUrl];
    	
    	//adding header information:
    	[postRequest setHTTPMethod:@"POST"];
    	
    	NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
    	[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
    	
    	
    	//setting up the body:
    	NSMutableData *postBody = [NSMutableData data];
    	[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"category\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"iPhone"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"format\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
 
// [CORRECTED] It was like this
//   	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] //dataUsingEncoding:NSUTF8StringEncoding]];
 
//NOW
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:self.theCaption] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    	[postBody appendData:[NSData dataWithData:UIImageJPEGRepresentation(resizedImage(self.myPhoto, CGRectMake(0, 0, 600, 800)), 0.5)]];
    	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 
    	[postRequest setHTTPBody:postBody];
    	
    	NSError *theError;
    	NSData *returnData = [ NSURLConnection sendSynchronousRequest: postRequest returningResponse: nil error:&theError ];
    	NSString *returnDataString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    	
    	NSLog(@"[DEBUG]... response from request: %@", returnDataString);
ASKER CERTIFIED SOLUTION
Cuahutleco

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros