0

Client ask me to POST some data fields: type, description, email, phone, price, location, photo (file attachment). Mime-Type: multipart/form-data

I have success with all fields except images.

I am using next code:

    UIImage *testImage = [UIImage imageNamed:@"landish.jpeg"];
    NSDictionary *parametres = @{@"type" : @"Suche",
                             @"description" : @"Flowers",
                             @"email" : @"myemail@ya.ru",
                             @"phone" : @"+565124512645",
                             @"price" : @"100",
                             @"location" : @"Gorppingen",
                             @"image" : testImage};

    UIImage *image = param[@"image"];
    NSURL *url = [NSURL URLWithString:API_URL_BASE];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    NSMutableDictionary *mutableDict = param.mutableCopy;
    [mutableDict removeObjectForKey:@"image"];
    param = mutableDict;
    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
                                    path:@"anzeigen.php" 
                                    parameters:param 
                                    constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"landish" fileName:@"landish.jpeg" mimeType:@"image/jpeg"];
    // etc.
}];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Request Successful, response '%@'", responseStr);
        completition (YES);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
        completition (NO);
    }];
    [httpClient enqueueHTTPRequestOperation:operation];

I send successfully text fields, but image not shown in iPhone, that must work, because from Android app it's sending successfully for the same API, but I cann't talk with that developer

If I try use nil parameters and send only image

 NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" 
                                 path:@"anzeigen.php" 
                                 parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"landish" fileName:@"landish.jpg" mimeType:@"image/jpeg"];
    // etc.
}];

I take next in log

 2013-10-23 02:41:26.345 ****[2280:c07] [HTTPClient Error]: Expected status code in (200-299), got 400

Maybe you know what I make wrong?

4

0 回答 0