Objective-c HTTP Basic authentication

Alex picture Alex · Nov 8, 2010 · Viewed 11.3k times · Source

How would i go about replicating this in objective-c

curl -u [email protected]:mypassword http://foo.lighthouseapp.com/projects.xml

I've been playing around with the ASIHTTPRequest library but can't seem to figure it out,

obviously sending my request like so returns an authentication error:

-(void)submit:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://ldn.lighthouseapp.com/projects/63254-londonist-20/tickets.xml"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        NSLog(@"response:%@",response);
    } else {
        NSLog(@"error:%@",error);
    }
}

This is probably very simple i'm just missing something pretty major

Answer

Aaron Saunders picture Aaron Saunders · Nov 8, 2010

From ASIHTTPRequest Documentation

With a Request Header

[request addRequestHeader:@"Authorization"
                    value:[NSString stringWithFormat:@"Basic %@",
                           [ASIHTTPRequest base64forData:
                            [[NSString stringWithFormat:@"%@:%@", theUsername, thePassword]
                             dataUsingEncoding:NSUTF8StringEncoding]]]];