Obj-c AFNetworking 2.0 POST request not working

Hi, I want to send some data (lines and file) to the server using AFNetworking 2.0. Somehow the data for the POST request (for forumlar) is incorrect, it seems that the encoding / serialization on request is missing. Because the server cannot work with the data I uploaded.

How to set encoding / serialization in a request?

I assume that the URL form parameter encoding should be set. The docs indicated

[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters]; 

I tried to do this, but I can’t figure out how to do it right. The following Xcode generates a warning:

 manager.requestSerializer = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters]; 

/.../CameraViewController.m: 105: 31: Incompatible pointer types that assign 'AFHTTPRequestSerializer *' from 'NSMutableURLRequest *'

Below is my source code:

CameraViewController.h

 #import <UIKit/UIKit.h> @interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end 

CameraViewControllerView.m

 #import "CameraViewController.h" #import "AFHTTPRequestOperationManager.h" @interface CameraViewController () @property (nonatomic) int photoIsTaken; @end @implementation CameraViewController // removed unecessary code for this question - (void)upload { NSLog(@"%s: uploader ", __FUNCTION__); AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@"latitude": @"8.444444", @"longitude": @"50.44444", @"location": @"New York", @"type": @"2", @"claim": @"NYC", @"flag": @"0", @"file": UIImageJPEGRepresentation(self.imageView.image,0.2)}; NSString *URLString = @"http://192.168.1.157/tapp/laravel/public/foobar/upload"; manager.requestSerializer = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters]; [manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@, %@", error, operation.responseString); }]; [self dismissViewControllerAnimated:NO completion:nil]; } @end 
+9
post ios objective-c request afnetworking-2
Nov 22 '13 at 11:35
source share
3 answers

Finally, it works. It was a hassle, but now I'm really happy ... During my testing, I had problems with the "request for the body thread exhausted" in Wifi, which was strange.

Below is the code that helped.

 - (void)upload { // !!! only JPG, PNG not covered! Have to cover PNG as well NSString *fileName = [NSString stringWithFormat:@"%ld%c%c.jpg", (long)[[NSDate date] timeIntervalSince1970], arc4random_uniform(26) + 'a', arc4random_uniform(26) + 'a']; // NSLog(@"FileName == %@", fileName); AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@"lat": @"8.444444", @"lng": @"50.44444", @"location": @"New York", @"type": @"2", @"claim": @"NYC", @"flag": @"0"}; // BASIC AUTH (if you need): manager.securityPolicy.allowInvalidCertificates = YES; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"foo" password:@"bar"]; // BASIC AUTH END NSString *URLString = @"http://192.168.1.157/tapp/laravel/public/foobar/upload"; /// !!! only jpg, have to cover png as well NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.5); // image size ca. 50 KB [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure %@, %@", error, operation.responseString); }]; [self dismissViewControllerAnimated:NO completion:nil]; } 
+6
Nov 25 '13 at 10:54
source share

Thanks to @NobleK, a category might be the best way to fix this problem. Here is a sample code:

 @interface AFURLConnectionOperation (AuthenticationChallengeUploadFix) @end @implementation AFURLConnectionOperation (AuthenticationChallengeUploadFix) - (NSInputStream *)connection:(NSURLConnection __unused *)connection needNewBodyStream:(NSURLRequest *)request { if ([request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) { return [request.HTTPBodyStream copy]; } return nil; } @end 
+6
Sep 29 '14 at 7:11
source share

I have been looking for a good answer for this damn problem for over 10 hours and finally figured it will work! according to Apple Doc

NSURLErrorRequestBodyStreamExhausted (-1021) Returns when the body stream is needed, but the client does not provide it. This affects iOS clients who send a POST request using the body stream but do not implement the NSURLConnection: needNewBodyStream delegate method connection.

so I needed to subclass AFHTTPRequestOperation and implement all delegate methods for NSURLConnection //. H

  @interface CGHTTPRequestOperation : AFHTTPRequestOperation @end 

// m.

  @implementation CGHTTPRequestOperation #pragma mark NSURLConnection delegate methods - (NSInputStream *)connection:(NSURLConnection __unused *)connection needNewBodyStream:(NSURLRequest *)request { if ([request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) { return [request.HTTPBodyStream copy]; } return nil; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [super connection:connection didReceiveResponse:response]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [super connection:connection didReceiveData:data]; } - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite { [super connection:connection didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite]; } - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{ return [super connection:connection willCacheResponse:cachedResponse]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ [super connectionDidFinishLoading:connection]; } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [super connection:connection didFailWithError:error]; } - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection { return YES; } - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [super connection:connection willSendRequestForAuthenticationChallenge:challenge]; } @end 

If you're curious how to use these extended classes to load data with multiple parts, here is an example //. H

  typedef enum { CGFileUploadStatusError = 0, CGFileUploadStatusSuccess = 1, } CGFileUploadStatus; typedef void(^CGNetworkingFileUploadCBlock) (CGFileUploadStatus status,NSString *responseString); 

// m.

  + (void) uploadImageAtPath:(NSString *) imagePath cBlock:(CGNetworkingFileUploadCBlock) cBlock { AFHTTPRequestSerializer *r = [AFHTTPRequestSerializer serializer]; NSDictionary *param = @{@"param1":@"",@"param2":@""}; NSData *d = [NSData dataWithContentsOfFile:imagePath]; __block NSString *paramNameForImage = [imagePath pathComponents].lastObject; NSError *error = nil; NSMutableURLRequest *urlRequest = [r multipartFormRequestWithMethod:@"POST" URLString:@"http://url_to_up_load_image" parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:d name:@"FileUploadControl" fileName:paramNameForImage mimeType:@"image/jpeg"]; } error:&error]; if (error) { NSLog(@"Some error:%@",error); } CGHTTPRequestOperation *requestOperation = [[CGHTTPRequestOperation alloc] initWithRequest:urlRequest]; //[requestOperation setCredential:nil]; //set cred here //[requestOperation setSecurityPolicy:nil]; //set security policy here if you are using one [requestOperation setResponseSerializer:[AFHTTPResponseSerializer serializer]]; [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@ ***** %@", operation.responseString, operation.response.allHeaderFields); cBlock(CGFileUploadStatusSuccess,operation.responseString); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@ ***** %@", operation, error); cBlock(CGFileUploadStatusError,operation.responseString); }]; [requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { }]; [requestOperation start]; [requestOperation waitUntilFinished]; } 

Hope this helps those who suffer from this problem :)

+4
Jan 23 '14 at 9:25
source share



All Articles