Dart
Dart
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 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;
}
// Return the value of a response header field by name. Header field names are case-insensitive.
final String contentType;
try {
contentType = rest.responseHdrByName('Content-Type');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Content-Type: $contentType');
}