Reputation: 45
In order to add a subscriber to MailChimp list using API 3.0 the parameters e-mailadress and status are required. If the request succeeds the response will contain the id in json-format.
My problem is that when I use my Member Class to add a subscriber the request sends the id, which is null, and therefore I get the error Bad Request. How can I change my Member class to only read the id from the response and not add it in the request? I'm using RestSharp, Visual Studio 2013, .Net 4.0,
My member class looks like this
public class Member
{
public Member(){}
public Member(string status)
{
this.status = status;
}
public Member(string email_address, string status)
{
this.status = status;
this.email_address = email_address;
}
public string id { get; set; }
public string email_address { get; set; }
public string status { get; set; }
}
And my createMember function looks like this:
static void createMember()
{
var client = new RestClient();
client.BaseUrl = new Uri("https://" + dc + ".api.mailchimp.com/3.0");
//HTML Basic Authentification
client.Authenticator = new HttpBasicAuthenticator(usernsme, apiKey);
var request = new RestRequest(Method.POST);
request.Resource = "lists/{list_id}/members";
request.AddUrlSegment("list_id", listId);
request.RequestFormat = DataFormat.Json;
request.AddBody(new Member(memberEmail, "subscribed"));
RestClient.AddHandler();
IRestResponse<Member> response2 = client.Execute<Member>(request);
string _id = response2.Data.id;
Console.WriteLine(response.StatusCode);
Console.WriteLine(_id);
}
I can add a subscriber to a list if I remove the id from the Member Class but how can then get the Id from the response and work with it?
Upvotes: 2
Views: 1353
Reputation: 14614
According to their reference, the API method you're calling has different the request body parameters and response body parameters, so I'd suggest using two different classes. The first one would be MemberRequest
, which is for the request and doesn't have the id
field
public class MemberRequest
{
public MemberRequest(){}
public MemberRequest(string status)
{
this.status = status;
}
public MemberRequest(string email_address, string status)
{
this.status = status;
this.email_address = email_address;
}
public string email_address { get; set; }
public string status { get; set; }
}
and the second one would be MemberResponse
, which is for the response and has the id
field
public class MemberResponse
{
public string id { get; set; }
public string email_address { get; set; }
public string status { get; set; }
}
Here's how your createMember
method would look like
static void createMember()
{
var client = new RestClient();
client.BaseUrl = new Uri("https://" + dc + ".api.mailchimp.com/3.0");
//HTML Basic Authentification
client.Authenticator = new HttpBasicAuthenticator(usernsme, apiKey);
var request = new RestRequest(Method.POST);
request.Resource = "lists/{list_id}/members";
request.AddUrlSegment("list_id", listId);
request.RequestFormat = DataFormat.Json;
request.AddBody(new MemberRequest(memberEmail, "subscribed"));
IRestResponse<MemberResponse> response = client.Execute<MemberResponse>(request);
string _id = response.Data.id;
Console.WriteLine(response.StatusCode);
Console.WriteLine(_id);
}
Upvotes: 3