Java Requires Chilkat v11.0.0+
Java
SendGrid: Save and verify a topic opt-out
See more SendGrid Examples
Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.
Add group suppressions ยท Check specific addresses
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Http http = new Http();
http.setRequireSslCertVerify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.setAuthToken("SENDGRID_API_KEY");
HttpResponse resp = new HttpResponse();
// Take the address from the authenticated account and the group ID from configuration.
String address = "reader@example.com";
http.setUrlVar("group", "12345");
try {
http.httpNoBody("GET", "https://api.sendgrid.com/v3/asm/groups/{$group}", resp);
} catch (ChilkatException e) {
if (resp.getStatusCode() == 0) {
System.out.println(e.getLastErrorText());
} else {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
}
return;
}
if (resp.getStatusCode() != 200) {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
return;
}
JsonObject groupInfo = new JsonObject();
try {
groupInfo.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Unexpected response: expected JSON.");
return;
}
if (!groupInfo.stringOfEquals("name", "Product updates", true)) {
System.out.println("Unexpected unsubscribe group; no preference was changed.");
return;
}
JsonObject addresses = new JsonObject();
addresses.updateString("recipient_emails[0]", address);
try {
http.httpJson("POST", "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions", addresses, "application/json", resp);
} catch (ChilkatException e) {
if (resp.getStatusCode() == 0) {
System.out.println(e.getLastErrorText());
} else {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
}
return;
}
if (resp.getStatusCode() != 201) {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
return;
}
try {
http.httpJson("POST", "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search", addresses, "application/json", resp);
} catch (ChilkatException e) {
if (resp.getStatusCode() == 0) {
System.out.println(e.getLastErrorText());
} else {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
}
return;
}
if (resp.getStatusCode() != 200) {
System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
return;
}
JsonArray matches = new JsonArray();
try {
matches.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Unexpected response: expected JSON.");
return;
}
if (matches.getSize() == 1) {
System.out.println("Confirmed opt-out: " + matches.stringAt(0));
} else {
System.out.println("Opt-out not confirmed. Recheck this group.");
}
}
}