Question

So i've set up a POST request sending information about a user's name, email and password along with an image. The page im sending a request to has been coded to var_dump($_FILES), var_dump($_POST) and this information is returned and logged. However, the var_dump() shows that it has not received a file or any POST data. The error I am assuming must be in the way the POST request has been coded, however I can't see what the error is:

    NSString *name = self.step1view.name.text;
    NSString *email = self.step2view.email.text;
    NSString *password = self.step3view.password2.text;
    NSLog([NSString stringWithFormat:@"name: %@; email: %@; password: %@",name,email,password]);
    //create URL
    NSURL *postURL = [NSURL URLWithString:URL];

    //Create post request connection
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0];

    [postRequest setHTTPMethod:@"POST"];
    NSString *stringBoundary = @"ghkyre–nhjfhdj-74f5f-gfg5-gggff";

    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];

    [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];

    NSMutableData *postBody = [NSMutableData data];

    //USERNAME
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Disposition: form-data; name=\"name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@",name] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    //EMAIL
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Disposition: form-data; name=\"email\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@",email] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    //PASSWORD
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Disposition: form-data; name=\"password\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@",password] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    //IMAGE
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.jpg\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:imageData];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [postRequest setHTTPBody:postBody];

    //send request
    NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"response: %@",returnString);
Was it helpful?

Solution

Three things:

  1. When setting the Content-Typeheader, use setValue:forHTTPHeaderField rather than addValue:forHTTPHeaderField.

  2. There's a superfluous CRLF for the image parameter:

    This:

    [postBody appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.jpg\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    

    should be

    [postBody appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    
  3. Don't pass nil to sendSynchronousRequest:returningResponse:error for either the response or error parameters. Both of these will hold vital debugging information after the request has been made.

UPDATE: Your boundary ghkyre–nhjfhdj-74f5f-gfg5-gggff contains a Unicode character . Replace it with - and you can access the POST data via PHP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top