Dart
Dart
Set a Multipart Part Body from a StringBuilder
See more REST Examples
Demonstrates Rest.SetMultipartBodySb, which sets the text body of the selected multipart part from a StringBuilder.
Background. A multipart request is assembled part by part: the part selector chooses the part being built, header fields describe it, and a SetMultipartBody method supplies its content. The request is then sent with a multipart send or full-request method.
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;
}
// Select the multipart part to build, and set its header fields.
rest.partSelector = '1';
rest.addHeader('Content-Disposition', 'form-data; name="field1"');
rest.addHeader('Content-Type', 'application/json');
// Set the text body of the selected part from a StringBuilder.
final sbBody = CkStringBuilder();
sbBody.append('{ "key": "value" }');
try {
rest.setMultipartBodySb(sbBody);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final String responseText;
try {
responseText = rest.fullRequestMultipart('POST', '/api/upload');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print(responseText);
}