Created
June 29, 2012 21:18
-
-
Save soopercorp/3020706 to your computer and use it in GitHub Desktop.
Simple REST calls with Objective-c
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
NSString *twitterReqUrl = @"http://api.twitter.com/1/statuses/user_timeline.atom?screen_name=hardikr"; | |
NSString *resp = [self makeRestCall:twitterReqUrl]; |
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
-(NSURLRequest*) requestWithHeader : (NSString*) reqURL : (NSString*) header : (NSString*) headerField | |
{ | |
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:reqURL]]; | |
// header could be defined as something like this: | |
// NSString *authHeader = @"Auth: api_key=343535353&auth_token=1267182121"; | |
// headerField could be @"Authorization:" | |
[mutableRequest addValue:authHeader forHTTPHeaderField:headerField]; | |
return (NSURLRequest*) mutableRequest; | |
} |
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
-(NSString*) makeRestCall : (NSString*) reqURL | |
{ | |
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]]; | |
NSURLResponse *resp = nil; | |
NSError *error = nil; | |
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &error]; | |
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; | |
NSLog(@"%@",responseString); | |
return responseString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment