Question

post image and test using multipart, but it gave me error Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request ,i am not able to post please help thanks in advance

NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] init];
[post_dict setObject:Name.text forKey:@"name"];
[post_dict setObject:ContactNameTF.text forKey:@"contactname"];
[post_dict setObject:EmailTf.text forKey:@"email"];
[post_dict setObject:PasswordTF.text forKey:@"password"];
[post_dict setObject:HouseNumberTF.text forKey:@"housenumber"];
[post_dict setObject:StreetTF.text forKey:@"street"];
[post_dict setObject:cityTF.text forKey:@"city"];
[post_dict setObject:POstcodeTF.text forKey:@"postalcode"];
[post_dict setObject:ContactNumberTF.text forKey:@"contact"];
//[post_dict setObject:self.theImage forKey:@"image_url"];

NSString *urlString = @"http://www.ofertas24.net/codegen/index.php/codegen/register_post";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[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"];

// add the image form fields
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image_url\"; filename=\"%@image.jpg\"\r\n",NameTF.text ] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:self.theImage];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add the text form fields
for (id key in post_dict) {
    NSLog(@"%@",key);
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[post_dict objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}

// close the form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// set request body
[request setHTTPBody:body];

// send the request (submit the form) and get the response
NSOperationQueue *queue =[[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
 {

     NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
     if ([result isEqualToString:@"true"]) {
         [self performSelectorOnMainThread:@selector(mainViewController) withObject:nil waitUntilDone:NO];
     }
     else {

         NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
     }

 }];
Was it helpful?

Solution

try this code,hope it helps you. . . . . . .. . . . . ..

    NSString *baseurl = @"Enter URL here";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];

//Create boundary, it can be anything
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] init];
[post_dict setObject:Name.text forKey:@"name"];
[post_dict setObject:ContactNameTF.text forKey:@"contactname"];
[post_dict setObject:EmailTf.text forKey:@"email"];
[post_dict setObject:PasswordTF.text forKey:@"password"];
[post_dict setObject:HouseNumberTF.text forKey:@"housenumber"];
[post_dict setObject:StreetTF.text forKey:@"street"];
[post_dict setObject:cityTF.text forKey:@"city"];
[post_dict setObject:POstcodeTF.text forKey:@"postalcode"];
[post_dict setObject:ContactNumberTF.text forKey:@"contact"];
[post_dict setObject:self.theImage forKey:@"image_url"];



// add params (all params are strings)
for (NSString *param in post_dict) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [post_dict objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

NSString *FileParamConstant = @"Enter Key Of Image Paramater";

NSData *imageData = UIImageJPEGRepresentation(image, 1);

//Assuming data is not nil we add this to the multipart form
if (imageData)
{
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the request
[request setHTTPBody:body];

// set URL
[request setURL:[NSURL URLWithString:baseurl]];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                           if ([result isEqualToString:@"true"]) {
                               [self performSelectorOnMainThread:@selector(mainViewController) withObject:nil waitUntilDone:NO];
                           }
                           else {

                               NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
                           }

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