Dart
Dart
REST POST JSON using Gzip Content Encoding
See more REST Examples
Demonstrates how to send a JSON POST using the gzip Content-Encoding.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Use the online tool at https://tools.chilkat.io/Default.cshtml
// to generate the JSON code that creates this JSON:
// {
// "lists": [
// {
// "id": "1511199999"
// }
// ],
// "confirmed": false,
// "email_addresses": [
// {
// "email_address": "support@chilkatsoft.com"
// }
// ],
// "first_name": "Matt",
// "last_name": "Smith"
// }
//
final json = CkJsonObject();
json.updateString('lists[0].id', '1511199999');
json.updateBool('confirmed', false);
json.updateString('email_addresses[0].email_address', 'support@chilkatsoft.com');
json.updateString('first_name', 'Matt');
json.updateString('last_name', 'Smith');
final rest = CkRest();
try {
rest.connect('api.constantcontact.com', 443, true, true);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
rest.addHeader('Content-Type', 'application/json');
rest.addHeader('Authorization', 'Bearer xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx');
// Tell the server you'll accept only an application/json response.
rest.addHeader('Accept', 'application/json');
// Indicate that we'll be sending the request body gzipped.
rest.addHeader('Content-Encoding', 'gzip');
final sbReq = CkStringBuilder();
json.emitSb(sbReq);
// Send the POST.
final sbResp = CkStringBuilder();
try {
rest.fullRequestSb('POST', '/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=xxxxxxx', sbReq, sbResp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Response body:');
print(sbResp.getAsString());
if (rest.responseStatusCode != 200) {
print('Received error response code: ${rest.responseStatusCode}');
return;
}
final jsonResp = CkJsonObject();
jsonResp.loadSb(sbResp);
// Use the online tool at https://tools.chilkat.io/jsonParse.cshtml
// to generate the JSON code that parses the JSON response..
}