Objective-C
Objective-C
Enumerate REST Response Header Names
See more REST Examples
Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.
Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.
Chilkat Objective-C Downloads
#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;
}
// Enumerate the response header fields by zero-based index. ResponseHdrName returns the field name
// and ResponseHdrValue returns the value at the same index.
int numHeaders = [rest.NumResponseHeaders intValue];
int i;
for (i = 0; i <= numHeaders - 1; i++) {
NSString *name = [rest ResponseHdrName: [NSNumber numberWithInt: i]];
if (rest.LastMethodSuccess == NO) {
NSLog(@"%@",rest.LastErrorText);
return;
}
NSString *value = [rest ResponseHdrValue: [NSNumber numberWithInt: i]];
if (rest.LastMethodSuccess == NO) {
NSLog(@"%@",rest.LastErrorText);
return;
}
NSLog(@"%@%@%@",name,@": ",value);
}