'How to extract specific fields in BATCH using Mailchimp API v3 for GET method?

I am trying to GET information(specific fields from email-activity), how should I do it in Batch GET requests in Mailchimp api v3? This is my code -

{
"operations": [
  {
    "method": "GET", 
    "path": "/reports/campaign_id/email-activity",
   "operation_id" : "123"
  }
]
}

With the above code, the entire email-activity is extracted, I only need few fields from email-activity like "email_address" and "campaign_id". How do I do it using BATCH GET request using Mailchimp API v3?



Solution 1:[1]

The answer is found in the Getting Started Guide.

Partial responses

Use field parameters to cut down on data transfers by limiting which fields the MailChimp API returns. For example, you may not need the full details of a resource, and can instead pass a comma-separated list of specific fields you want to include.

The parameters fields and exclude_fields are mutually exclusive and will throw an error if a field isn’t valid in your request. For example, the following URL uses the fields query string parameter to only include the list name and list id fields in the response:

https://usX.api.mailchimp.com/3.0/lists?fields=lists.name,lists.id

Now, you might be wondering, "How do I add parameters to a batch request?" Fortunately, the MailChimp documentation is here for you. Refer to the How to use Batch Operations guide, which tells you to include an field called params in your operation object.

In the example above, you would do the following:

{
  "operations": [{
      "method": "GET", 
      "path": "/reports/campaign_id/email-activity",
      "params": {
        "fields": "campaign_id,emails.email_address"
      },
      "operation_id" : "123"
  }]
}

Note: You say you'd like to retrieve the campaign_id field. Please note that the "path" portion of the request already needs to contain the campaign_id, so you have to have it before you can make this request at all. That said, you might find it valuable to be included in the response so that your processor doesn't have to have information about the request that generated the response.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gbeaven