Sample code for 30+ languages & platforms
Objective-C

Get a REST Response Header by Name

See more REST Examples

Demonstrates Rest.ResponseHdrByName, which returns the value of a response header field by name. Header field names are case-insensitive.

Background. Response headers are available after ReadResponseHeader has read the status line and headers. Lookup by name is convenient when a specific header, such as Content-Type, is needed.

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;
}

success = [rest SendReqNoBody: @"GET" uriPath: @"/api/items/123"];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

int statusCode = [[rest ReadResponseHeader] intValue];
if (statusCode < 0) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

//  Return the value of a response header field by name.  Header field names are case-insensitive.
NSString *contentType = [rest ResponseHdrByName: @"Content-Type"];
if (rest.LastMethodSuccess == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

NSLog(@"%@%@",@"Content-Type: ",contentType);