I'm trying to setup a basic API request using AFNetworking
in Objective C. However, I'm a little confused about how the AFHTTPRequestSerializer
is supposed to work. As of now, the below code doesn't seem to execute a request to my API, as nothing is printed in either the success / error NSLog
statements. I don't think I understand exactly how AFHTTPRequestSerializer
is incorporated into AFHTTPRequestOperationManager
:
NSString *URLString = @"http://mysite.dev/v1/user/5";
NSDictionary *parameters = @{@"include": @"places"};
manager = [[AFHTTPRequestOperationManager alloc]
initWithBaseURL:[NSURL URLWithString:URLString]];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
// Do I need to return this? Or does AFHTTPRequestOperationManager
// use this implicitly within its request?
NSMutableURLRequest *request = [manager.requestSerializer
requestWithMethod:@"GET"
URLString:URLString
parameters:parameters
error:nil
];
// My oAuth Header token
[manager.requestSerializer setValue:@"1234" forHTTPHeaderField:@"Authorization"];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager HTTPRequestOperationWithRequest: request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError * error) {
NSLog(@"%@", error);
}];
Try with following function it works for me. I settle out Header Value
and used requestSerializer
- (void)getServerRequest:(NSString *)strRequestURL {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"Header1Value" forHTTPHeaderField:@"Header1Key"];
[manager GET:@"URL String" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}