Dart
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final rest = CkRest();
final bTls = true;
final bAutoReconnect = true;
try {
rest.connect('example.com', 443, bTls, bAutoReconnect);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
rest.sendReqNoBody('GET', '/api/items/123');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final statusCode = rest.readResponseHeader();
if (statusCode < 0) {
print(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.
final numHeaders = rest.numResponseHeaders;
for (var i = 0; i < numHeaders; i++) {
final String name;
try {
name = rest.responseHdrName(i);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final String value;
try {
value = rest.responseHdrValue(i);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('$name: $value');
}
}