GNG
GNG

Reputation: 1531

Adding a new subscriber to a Mailchimp audience using Java and a Jersey Client

I need to add a subscriber to a mailchimp audience. I am using Java and a Jersey client. I have no trouble fetching the members of the audience using the Mailchimp 3.0 API. What must I change about my post request in order to successfully add a new subscriber? When my code fails, there is no response. Then, when I check the Mailchimp account and see that no new subscriber was added.

class MailchimpClient {

    Client client;
    String mailchimpUrl;

    public MailchimpClient() {
        String mailchimpApikey = getAPIKey();
        String datacenter = mailchimpApikey.substring(mailchimpApikey.lastIndexOf('-') + 1);
        mailchimpUrl = "https://" + datacenter + ".api.mailchimp.com/3.0/";

        JacksonJsonProvider jjp = new JacksonJsonProvider();
        jjp.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        ClientConfig conf = new ClientConfig();
        conf.property(ClientProperties.CONNECT_TIMEOUT, TIMEOUT);
        conf.property(ClientProperties.READ_TIMEOUT, TIMEOUT);
        conf.register(jjp);

        HttpAuthenticationFeature httpAuth = HttpAuthenticationFeature.basic("username", mailchimpApikey);

        client = ClientBuilder.newClient(conf).register(httpAuth);
    }

    public <T> T get(String path, Class<T> responseType) {
        T t = client.target(mailchimpUrl).path(path).request(MediaType.APPLICATION_JSON).get(responseType);
        client.close();
        return t;
    }

    public <T> T post(String path, Object payload, Class<T> responseType) {
        Entity<Object> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
        T t = client.target(mailchimpUrl).path(path).request(MediaType.APPLICATION_JSON).post(entity, responseType);
        client.close();
        return t;
    }

    String audienceid = xxxxxxx;
    MailchimpClient mcc = new MailchimpClient();
    MembersResponse mr = mcc.get("lists/" + audienceid + "/members", MembersResponse.class);
    addToMailchimpAudience(String audienceid);

private Message addToMailchimpAudience(String audienceid) {
    HashMap<String, String> newMember = new HashMap<String, String>();
    newMember.put("email_address", "[email protected]");
    newMember.put("status", "subscribed");
    MailchimpClient mcc = new MailchimpClient();
    MembersResponse mr = mcc.post("lists/" + audienceid + "/members", newMember, MembersResponse.class);
    logger.info("response: " + mr);
    return Message.success("Successfully added new member to mailchimp audience");

}


Upvotes: 0

Views: 935

Answers (1)

user3735341
user3735341

Reputation: 11

The request to add new members should be a JSON Add a Contact to a List/Audience

To add a contact to a list/audience, send a POST request to the List Members endpoint: /3.0/lists/9e67587f52/members/. The request body should be a JSON object that contains the information you want to add, with status and any other required list fields.

{ "email_address": "[email protected]", "status": "subscribed", "merge_fields": { "FNAME": "Urist", "LNAME": "McVankab" } }

I observed you are sending values in a HasMap, trying converting into a JSON and see if it works

Link for your reference: https://developer.mailchimp.com/documentation/mailchimp/guides/manage-subscribers-with-the-mailchimp-api/

Upvotes: 0

Related Questions