Sample code for 30+ languages & platforms
Objective-C

Get a REST Response Header Value by Index

See more REST Examples

Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.

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 the response header at a zero-based index.  Use the same index with
//  ResponseHdrName to get the corresponding field name.
int numHeaders = [rest.NumResponseHeaders intValue];
int i;
for (i = 0; i <= numHeaders - 1; i++) {
    NSString *value = [rest ResponseHdrValue: [NSNumber numberWithInt: i]];
    if (rest.LastMethodSuccess == NO) {
        NSLog(@"%@",rest.LastErrorText);
        return;
    }

    NSLog(@"%@%d%@%@",@"Header ",i,@" value: ",value);
}