Sample code for 30+ languages & platforms
Objective-C

Configure HTTP Basic Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthBasic, which configures HTTP Basic authentication from a username and password. Chilkat automatically adds the Authorization header to each request sent through the object.

Background. HTTP Basic authentication transmits credentials with every request, so it should be used over HTTPS. Credentials should be obtained from a secure source rather than hard-coded in production code.

Chilkat Objective-C Downloads

Objective-C
#import <CkoRest.h>
#import <NSString.h>

BOOL success = NO;

CkoRest *rest = [[CkoRest alloc] init];
BOOL bTls = YES;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"example.com" port: [NSNumber numberWithInt: 443] tls: bTls autoReconnect: bAutoReconnect];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

//  Normally you would not hard-code secrets in source.  You should instead obtain them from an
//  interactive prompt, environment variable, or a secrets vault.
NSString *username = @"myUsername";
NSString *password = @"myPassword";

//  Configure HTTP Basic authentication.  Chilkat automatically adds the Authorization header to
//  each request sent through this object.
success = [rest SetAuthBasic: username password: password];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

NSString *responseText = [rest FullRequestNoBody: @"GET" uriPath: @"/api/protected"];
if (rest.LastMethodSuccess == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

NSLog(@"%@",responseText);