Java
Chilkat2 Java
REST POST JSON using Gzip Content Encoding
See more REST Examples
Demonstrates how to send a JSON POST using the gzip Content-Encoding.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// 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"
// }
//
JsonObject 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");
Rest rest = new Rest();
try {
rest.connect("api.constantcontact.com", 443, true, true);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
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");
StringBuilder sbReq = new StringBuilder();
json.emitSb(sbReq);
// Send the POST.
StringBuilder sbResp = new StringBuilder();
try {
rest.fullRequestSb("POST", "/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=xxxxxxx", sbReq, sbResp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Response body:");
System.out.println(sbResp.getAsString());
if (rest.getResponseStatusCode() != 200) {
System.out.println("Received error response code: " + rest.getResponseStatusCode());
return;
}
JsonObject 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..
}
}