Dart
Dart
Read the REST Response Body into a StringBuilder
See more REST Examples
Demonstrates Rest.ReadRespSb, which reads the response body as text into a StringBuilder, replacing its previous contents.
Background. The staged request model separates sending from reading: a
SendReq method transmits the request, ReadResponseHeader reads the status line and headers, and a ReadResp method reads the body. This gives fine control over large or streamed messages compared with the single-call full-request methods.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;
}
// Read the response body as text into a StringBuilder, replacing its previous contents.
final sbResponse = CkStringBuilder();
try {
rest.readRespSb(sbResponse);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final String responseBody;
try {
responseBody = sbResponse.getAsString();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print(responseBody);
}