'Getting error "Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" in iOS 7
I am using AFNetworking version "2.5.4" and creating multipartform-data request. In my case code is working fine on iOS8 but issue on iOS 7. Getting Error
"Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)"
UserInfo=0x19039c00 {com.alamofire.serialization.response.error.response= { URL: http://xxx/api/abc/PostApi } { status code: 400, headers { Connection = close; "Content-Type" = "application/json"; Date = "Wed, 20 May 2015 05:42:47 GMT"; Server = Apache; } }, NSErrorFailingURLKey=http://xxx/api/abc/PostApi }, NSLocalizedDescription=Request failed: bad request (400), com.alamofire.serialization.response.error.data=<7b227375 63636573 73223a66 616c7365 2c226d65 73736167 65223a22 4e6f2064 61746122 2c226461 7461223a 5b5d2c22 72657370 6f6e7365 5f636f64 65223a22 52433030 3033227d>}
This is my Code that have issue
`
@property (strong, nonatomic) NSURLSessionUploadTask *postUploadTask;
__block int i=1;
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:postParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (imageArray.count > 0) {
long dataLenght = 0.0;
for(UIImage *eachImage in imageArray)
{
NSData *imageData = UIImageJPEGRepresentation(eachImage, .5);
dataLenght = dataLenght + imageData.length;
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"image%d",i] fileName:[NSString stringWithFormat:@"image%d.jpg",i] mimeType:@"image/jpeg"];
i++;
}
}
} error:nil];
NSProgress *progress = nil;
self.postUploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
[progress removeObserver:object forKeyPath:kProgressFractionCompleted context:kPostUpdateTypePostUpdate];
completionBlock(responseObject, error, task);
}];
[self.postUploadTask resume];`
While this is working code on same api-:
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
AFHTTPRequestOperation *op = [manager POST:urlString parameters:postParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (imageArray.count > 0) {
long dataLenght = 0.0;
for(UIImage *eachImage in imageArray)
{
NSData *imageData = UIImageJPEGRepresentation(eachImage, .5);
dataLenght = dataLenght + imageData.length;
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"image%d",i] fileName:[NSString stringWithFormat:@"image%d.jpg",i] mimeType:@"image/jpeg"];
i++;
}
}
} success:^(AFHTTPRequestOperation *operation, id responseObject){
completionBlock(responseObject, nil, task);
[progress removeObserver:object forKeyPath:kProgressFractionCompleted context:kPostUpdateTypePostUpdate];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completionBlock(operation, error, task);
}];
[op start];
But My requirement is to implement uploadTaskWithStreamedRequest. This is similar to issue"Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)".
Please help me guys if have any idea about it. Thanks in advance.
Solution 1:[1]
You should try adding request serializer to your AFHTTPRequestOperationManager.
AFJSONRequestSerializer *jsonRequestSerializer = [AFJSONRequestSerializer serializer];
[self.requestOperationManager setRequestSerializer:jsonRequestSerializer];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Say2Manuj |