Sample code for 30+ languages & platforms
React Native

REST POST JSON using Gzip Content Encoding

See more REST Examples

Demonstrates how to send a JSON POST using the gzip Content-Encoding.

Chilkat React Native Downloads

React Native
import { JsonObject, Rest, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // 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"
  // }
  // 

  const json = new JsonObject();
  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');

  const rest = new Rest();

  try {
    await rest.connectAsync('api.constantcontact.com', 443, true, true);
  } catch {
    console.log(rest.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');

  const sbReq = new StringBuilder();
  json.emitSb(sbReq);

  // Send the POST.
  const sbResp = new StringBuilder();
  try {
    await rest.fullRequestSbAsync('POST', '/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=xxxxxxx', sbReq, sbResp);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  console.log('Response body:');
  console.log(sbResp.getAsString());

  if (rest.responseStatusCode !== 200) {
    console.log(`Received error response code: ${rest.responseStatusCode}`);
    return;
  }

  const jsonResp = new JsonObject();
  jsonResp.loadSb(sbResp);

  // Use the online tool at https://tools.chilkat.io/jsonParse.cshtml
  // to generate the JSON code that parses the JSON response..
}