iOS AFNetworking GET / POST Parámetros

Quiero hacer una request POST con AFNetworking que contiene los parameters GET y POST.

Estoy usando este código:

NSString *urlString = [NSString stringWithFormat:@"upload_stuff.php?userGUID=%@&clientGUID=%@", @"1234", [[UIDevice currentDevice] identifierForVendor].UUIDString]; NSString *newUrl = @"https://sub.domain.com"; NSURL *baseURL = [NSURL URLWithString:newUrl]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; [httpClient defaultValueForHeader:@"Accept"]; NSDictionary *getParams = [NSDictionary dictionaryWithObjectsAndKeys: @"1234", @"userGUID", [[UIDevice currentDevice] identifierForVendor].UUIDString, @"clientGUID", nil]; NSDictionary *postParams = [NSDictionary dictionaryWithObjectsAndKeys: [@"xyz" dataUsingEncoding:NSUTF8StringEncoding], @"FILE", nil]; [httpClient postPath:urlString parameters:postParams success:^(AFHTTPRequestOperation *operation, id responseObject) { }failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error retrieving data: %@", error); }]; 

Ahora tengo dos preguntas:

  • ¿Cómo puedo usar los dictionarys BOTH GET y POST en la misma request? Por el momento, estoy integrando el dictionary GET en la URL y utilizando solo el dictionary POST ( [httpClient postPath:...] )

  • Recibo un error del server que indica que falta el parámetro "FILE". Lamentablemente, no puedo examinar ningún logging del server (no mi server). Pero usando una NSURLConnection estándar, pude enviar requestes con el parámetro FILE a este server. Entonces, ¿qué está mal aquí?

Stackoverflow para ti:

 NSData* sendData = [self.fileName.text dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *sendDictionary = [NSDictionary dictionaryWithObject:sendData forKey:@"name"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:remoteUrl]; NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/photos" parameters:sendDictionary constructingBodyWithBlock:^(id <AFMultipartFormData>formData) { [formData appendPartWithFileData:photoImageData name:self.fileName.text fileName:filePath mimeType:@"image/jpeg"]; } ]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest]; [operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite); }]; [operation setCompletionBlock:^{ NSLog(@"%@", operation.responseString); //Gives a very scary warning }]; [operation start]; 

Por @Igor Fedorchuk de POST jpeg upload con AFNetworking

AFNetworking no tiene ningún método para configurar parametros GET y POST. Tienes que configurar GET params en tu url, y usar [AFHTTPClient requestWithMethod:path:parameters:] setup POST params.

 - (NSURLRequest *)requestForPath:(NSString *)path method:(NSString *)method { NSMutableString *pathWithGetParams = [NSMutableString stringWithString:path]; BOOL hasPathContainsQueryChar = [path rangeOfString:@"?"].location != NSNotFound; [pathWithGetParams appendString:hasPathContainsQueryChar ? @"&" : @"?"]; for (id key in self.getArguments.allKeys) { if ([key isKindOfClass:[NSString class]]) { NSString *value = self.getArguments[key]; if ([value isKindOfClass:[NSString class]]) { [pathWithGetParams appendString:[[self class] urlEncode:key]]; [pathWithGetParams appendString:@"="]; [pathWithGetParams appendString:[[self class] urlEncode:value]]; [pathWithGetParams appendString:@"&"]; } } } NSString *upperCaseMethod = [method uppercaseString]; BOOL isMethodInGet = [upperCaseMethod isEqualToString:@"GET"]; NSURLRequest *request = [[self shareAFClient] requestWithMethod:method path:pathWithGetParams parameters:isMethodInGet ? nil : self.postArguments]; return request; } + (NSString *)urlEncode:(NSString *)stringToEncode { return [self urlEncode:stringToEncode usingEncoding:NSUTF8StringEncoding]; } + (NSString *)urlEncode:(NSString *)stringToEncode usingEncoding:(NSStringEncoding)encoding { return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)stringToEncode, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", CFStringConvertNSStringEncodingToEncoding(encoding)); } + (NSString*)urlDecode:(NSString *)stringToDecode { return [self urlDecode:stringToDecode usingEncoding:NSUTF8StringEncoding]; } + (NSString*)urlDecode:(NSString *)stringToDecode usingEncoding:(NSStringEncoding)encoding { return (__bridge_transfer NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)stringToDecode, (CFStringRef)@"", CFStringConvertNSStringEncodingToEncoding(encoding)); }