I have been working on a Mac App, and I am trying to make an Get HTTP request from github API, how ever this request is a conditional request and it looks something like this:
https://api.github.com/repos/soviettoly/sandbox/events -H "If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT"
When I do a curl -i on that request I get everything I want. However, I have been trying to this in XCode and I get a 404 back from github.
This is how I am making the request:
NSMutableString * theURL = [[NSMutableString alloc]initWithString:@"https://api.github.com/repos/soviettoly/sandbox/events -H \"If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT\""];
NSLog(@"the normal %@",theURL);
NSString * escaped = [theURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"the escpaed %@", escaped);
NSURL * test = [NSURL URLWithString: escaped];
NSLog(@"actual URL %@",test);
NSURLRequest * request = [NSURLRequest requestWithURL:test];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
The print out from the NSLog commands give me this:
the normal https://api.github.com/repos/soviettoly/sandbox/events -H "If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT"
the escpaed https://api.github.com/repos/soviettoly/sandbox/events%20-H%20%22If-Modified-Since:%20Sat,%2013%20Oct%202012%2023:35:10%20GMT%22
actual URL https://api.github.com/repos/soviettoly/sandbox/events%20-H%20%22If-Modified-Since:%20Sat,%2013%20Oct%202012%2023:35:10%20GMT%22
I am not sure why the curl command gives me back the correct result while making a request in XCode does not. I have tried without doing the escaped characters but XCode does not like the URL since it contains ilegal characters. I am not sure how to make this kind of call in XCode. I been making other API calls for GitHub no problem, I am just having trouble with this one. If anyone can hep that would be great. Thanks alot!
Presumably, the
curlcommand you’re using isThat is not requesting the page ‘https://api.github.com/repos/soviettoly/sandbox/events -H “If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT”‘, it is requesting the page ‘ https://api.github.com/repos/soviettoly/sandbox/events‘, and sending an additional HTTP header (
-H) that contains “If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT”.Your objective-c code is requesting the page ‘https://api.github.com/repos/soviettoly/sandbox/events -H ” If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT”‘. You need to use an
NSMutableURLRequestand set it to include the headerIf-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMTin a request tohttps://api.github.com/repos/soviettoly/sandbox/events.e.g.