brandonseydel/MailChimp.Net

BatchRequest: Adding new members - status_if_new not included in JSON

Closed this issue · 4 comments

Hi,

I am trying to use the Batch API to add or update members. For new members i am getting an The value you selected is not a valid choice. error on the status value viewed in the batch logs.

[{
	"status_code": 400,
	"operation_id": "",
	"response": "{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Invalid Resource\",\"status\":400,\"detail\":\"The resource submitted could not be validated. For field-specific details, see the 'errors' array.\",\"instance\":\"\",\"errors\":[{\"field\":\"status\",\"message\":\"The value you selected is not a valid choice.\"}]}"
}]

Example Code:

 Dim newMembers = New List(Of Member)()
        Dim fields As New Dictionary(Of String, Object)

        fields.Add("FNAME", "Anew")
        fields.Add("LNAME", "Member")

        Dim tags As New List(Of MemberTag)
        tags.Add(New MemberTag() With {.Name = "Test"})

        Dim newMember = New Member With {
        .EmailAddress = "test2@test.com",
        .StatusIfNew = Status.Subscribed,
        .MergeFields = fields,
        .Tags = tags
        }
        newMembers.Add(newMember)

        Dim batchRequest = New BatchRequest With {
            .Operations = newMembers.[Select](Function(x) New Operation With {
                .Method = "PUT",
                .Path = $"/lists/{ListID}/members/{manager.Members.Hash(x.EmailAddress.ToLower())}",
                .Body = JsonConvert.SerializeObject(x, serializerSettings)
            })
        }
        Dim batch = manager.Batches.AddAsync(batchRequest).Result

JSON request serialized by HttpRequestExtensions.PostAsJsonAsync

{
	"operations": [{
		"method": "PUT",
		"path": "/lists/<listId>/members/f2c97b1f2d2898cd2d6466ce95d4ba33",
		"params": {},
		"body": "{\"email_address\":\"test2@test.com\",\"interests\":{},\"_links\":[],\"marketing_permissions\":[],\"merge_fields\":{\"FNAME\":\"Anew\",\"LNAME\":\"Member\"},\"tags\":[\"Test\"],\"status\":\"\"}",
		"operation_id": ""
	}]
}

The operation body does not include "status_if_new":"subscribed" property.

The Members.AddOrUpdateAsync() method includes the "status_if_new":"subscribed" property in the request and i do not receive the error above from the MailChimp API.

Example:

  Dim newMembers = New List(Of Member)()
        Dim fields As New Dictionary(Of String, Object)

        fields.Add("FNAME", "Anew")
        fields.Add("LNAME", "Member")

        Dim tags As New List(Of MemberTag)
        tags.Add(New MemberTag() With {.Name = "Test"})

        Dim newMember = New Member With {
        .EmailAddress = "test2@test.com",
        .StatusIfNew = Status.Subscribed,
        .MergeFields = fields,
        .Tags = tags
        }

        Dim result = manager.Members.AddOrUpdateAsync(ListID, newMember).Result

JSON request serialized by HttpRequestExtensions.PutAsJsonAsync

{
	"email_address": "test2@test.com",
	"interests": {},
	"_links": [],
	"marketing_permissions": [],
	"member_rating": 0,
	"merge_fields": {
		"FNAME": "Anew",
		"LNAME": "Member"
	},
	"tags_count": 0,
	"tags": ["Test"],
	"status": "",
	"status_if_new": "subscribed",
	"vip": false
}

I would expect the serialization of the member object for the batch request would behave the same way as the single member update in Members.AddOrUpdateAsync().

Is there any reason why the status_if_new property is not included in the BatchRequest Operation when adding or updating Members?

Thanks,
Sean

it appears to be the serializerSettings and setting the serializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore.

The StatusIfNew = Status.Subscribed is not the default value however it's being ignored.

By moving Undefined in the Status enum to the 0 value the "status_if_new":"subscribed" is included in the serialization when serializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore is specified.

public enum Status
    {
        [Description("")]
        Undefined,

        [Description("subscribed")]
        Subscribed, 

        [Description("unsubscribed")]
        Unsubscribed, 

        [Description("cleaned")]
        Cleaned, 

        [Description("pending")]
        Pending,

        [Description("transactional")]
        Transactional,
        }

I am happy to open a PR?

Thanks

Go ahead and open a PR so I can review. Thanks

Stale issue message