Reputation: 1756
I'm using the following code to set up my operation manager:
requestSerializerJson = [AFJSONRequestSerializer serializer];
[requestSerializerJson setValue:[taskHandler getBranchesApiConstantForName:@"header_value_content_type"] forHTTPHeaderField:[taskHandler getBranchesApiConstantForName:@"header_name_content_type"]];
[requestSerializerJson setValue:[taskHandler getBranchesApiConstantForName:@"header_value_api_key"] forHTTPHeaderField:[taskHandler getBranchesApiConstantForName:@"header_name_api_key"]];
httpRequestOperationManager = [AFHTTPRequestOperationManager manager];
httpRequestOperationManager.requestSerializer = requestSerializerJson;
httpRequestOperationManager.responseSerializer = [AFJSONResponseSerializer serializer];
httpRequestOperationManager.operationQueue = [[NSOperationQueue alloc] init];
httpRequestOperationManager.operationQueue.maxConcurrentOperationCount = 1;
Then, I've got a method to download multiple JSON strings:
- (void)downloadBranches {
NSLog(@"Test %@", httpRequestOperationManager.requestSerializer.HTTPMethodsEncodingParametersInURI);
for (NSString *branchUuid in taskHandler.addedBranchesUuids) {
NSString *urlString = [NSString stringWithFormat:@"%@%@%@", urlBase, [taskHandler getBranchesApiConstantForName:@"getbranch_suffix"], branchUuid];
NSURL *URL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSLog(@"Test %@", request.allHTTPHeaderFields);
AFHTTPRequestOperation *op = [httpRequestOperationManager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[httpRequestOperationManager.operationQueue addOperation:op];
}
}
All the operations are performed but I get a message from the server which says that I didn't supply the authentication key.
I specifically set it in the 3rd line - for the requestSerializerJson
which is then added to the httpRequestOperationManager
so all of the requests should be using the headers.
I added an NSLog to print request.allHTTPHeaderFields
but it says "(null)".
How can I fix this?
Upvotes: 1
Views: 202
Reputation: 66242
downloadBranches
is completely bypassing the serializer by building an NSURLRequest
manually. The whole point of the serializer is to do that for you.
You need to call requestWithMethod:URLString:parameters:
on your serializer, which will return an NSURLRequest
with the headers you want.
Or just use the operation manager's GET:…
method inside that for
loop; your use case doesn't look like it requires this other stuff at all.
Upvotes: 1