Skip to content

Instantly share code, notes, and snippets.

@soopercorp
Created June 29, 2012 21:18
Show Gist options
  • Save soopercorp/3020706 to your computer and use it in GitHub Desktop.
Save soopercorp/3020706 to your computer and use it in GitHub Desktop.
Simple REST calls with Objective-c
NSString *twitterReqUrl = @"http://api.twitter.com/1/statuses/user_timeline.atom?screen_name=hardikr";
NSString *resp = [self makeRestCall:twitterReqUrl];
-(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;
}
-(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