Created
May 22, 2012 04:50
-
-
Save subdigital/2766667 to your computer and use it in GitHub Desktop.
Appending ?token=<auth_token> to the query params in AFNetworking for every request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method | |
path:(NSString *)path | |
parameters:(NSDictionary *)parameters { | |
NSString *token = [[PSCredentialRepository sharedRepository] authenticationToken]; | |
if ([method isEqualToString:@"POST"] || [method isEqualToString:@"PUT"]) { | |
if ([path rangeOfString:@"token="].location == NSNotFound) { | |
NSString *separator = @"&"; | |
if ([path rangeOfString:@"?"].location == NSNotFound) { | |
separator = @"?"; | |
} | |
path = [path stringByAppendingFormat:@"%@token=%@", separator, token]; | |
} | |
} else { | |
NSMutableDictionary *newParams = [NSMutableDictionary dictionaryWithDictionary:parameters]; | |
[newParams setObject:token forKey:@"token"]; | |
parameters = newParams; | |
} | |
return [super requestWithMethod:method path:path parameters:parameters]; | |
} |
As @mattt pointed out, I'd have to be careful not to ever include other query params in the path parameter. For some additional safeguard, this one will safely append using ?
or &
.
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters {
NSString *token = [[PSCredentialRepository sharedRepository] authenticationToken];
if ([path rangeOfString:@"token="].location == NSNotFound) {
NSString *separator = [path rangeOfString:@"?"].location == NSNotFound ? @"?" : @"&";
path = [path stringByAppendingFormat:@"%@token=%@", separator, token];
}
return [super requestWithMethod:method path:path parameters:parameters];
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, even better: