客户要求我发布一些数据fields: type, description, email, phone, price, location, photo (file attachment). Mime-Type: multipart/form-data
除了图像之外,我在所有领域都取得了成功。
我正在使用下一个代码:
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];
我成功发送了文字字段,但图片没有显示在iPhone中,必须有效,因为从Android应用程序中它成功发送了相同的API,但我无法与该开发人员交谈
如果我尝试使用nil参数并仅发送图像
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.
}];
我接下来的日志
2013-10-23 02:41:26.345 ****[2280:c07] [HTTPClient Error]: Expected status code in (200-299), got 400
也许你知道我做错了什么?