I am receiving a 400 bad request when I attempt to submit a HttpClient.PostAsJsonAsync() from my asp.NET MVC web application. I have the following class definitions public class ConstantContact
{
public ConstantContact()
{
Custom_Fields = new List<CustomField>();
Lists = new List<ContactList>();
Email_Addresses = new List<EmailAddress>();
}
public List<CustomField> Custom_Fields { get; set; }
public string Cell_Phone { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public List<ContactList> Lists { get; set; }
public List<EmailAddress> Email_Addresses { get; set; }
}
public class CustomField
{
public string name { get; set; }
public string value { get; set; }
}
public class ContactList
{
public string Id { get; set; }
}
public class EmailAddress
{
public string Opt_In_Source { get; set; }
public string Email_Address { get; set; }
} public class ContactModel { [StringLength(160, ErrorMessage = "{0} is too long.")] [Display(Name = "First Name")] public string FirstName { get; set; } [StringLength(160, ErrorMessage = "{0} is too long.")] [Display(Name = "Last Name")] public string LastName { get; set; } [Required(ErrorMessage = "{0} is required.")] [RegularExpression(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$", ErrorMessage = "{0} is invalid")] [Display(Name = "Email")] public string EmailAddress { get; set; } [Required(ErrorMessage = "{0} is required.")] [RegularExpression(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$", ErrorMessage = "{0} is invalid")] [Display(Name = "Confirm")] [Compare("EmailAddress", ErrorMessage = "Confirm does not match.")] public string ConfirmEmail { get; set; } [Display(Name = "Birth Date")] public string BirthDate { get; set; } public readonly string SuccessMessage = "<h4>Thank you!</h4>You have been added to our mailing list. Please check your inbox for a welcome message."; public readonly string FailureMessage = "<h4>Oops!</h4>Something went wrong with your message. Please try again later."; public readonly string ValidationMessage = "<h4>Oops!</h4>Please fix the errors and try again."; } When the form gets submitted the controller calls to the task layer to build the request as follows var contact = CreateConstantContact(model);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer [Access_Token]");
client.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/json");
var response = client.PostAsJsonAsync("https://api.constantcontact.com/v2/contacts?api_key=[api_key]", contact).Result;
if (response.IsSuccessStatusCode)
{
TempData["Feedback"] = model.SuccessMessage;
TempData["UpdateStatus"] = true;
}
else
{
TempData["Feedback"] = "Status: " + response.StatusCode + " - " + response.ReasonPhrase + "<br />" + response.RequestMessage.Content;
TempData["UpdateStatus"] = false;
} private ConstantContact CreateConstantContact(ContactModel model) { var contact = new ConstantContact(); if (string.IsNullOrEmpty(model.BirthDate) == false) { var customs = new CustomField { name = "CustomField1", value = model.BirthDate }; contact.Custom_Fields.Add(customs); } if (string.IsNullOrEmpty(model.FirstName) == false) { contact.First_Name = model.FirstName; } if (string.IsNullOrEmpty(model.LastName) == false) { contact.Last_Name = model.LastName; } var list = new ContactList { Id = "2" }; contact.Lists.Add(list); var email = new EmailAddress { Email_Address = model.EmailAddress, Opt_In_Source = "ACTION_BY_VISITOR" }; contact.Email_Addresses.Add(email); return contact; } I have tried a plethora of JSON serializers with absolutely no luck. Always receive 400 Bad Request. Is there something that I have not setup properly or am I missing something? Any suggestions will be much appreciated. Thanks, Will
... View more