Cannot create single Contact - getting a 400

mtrtm
Occasional Participant

Cannot create single Contact - getting a 400

Hi all,


I am new to Constant Contact and cannot create a contact.  I have looked at quite a bit of the documentation and .Net examples from http://developer.constantcontact.com/samples/upload_forms (which give me the same 400 error - that was after some modding since it didn't run out of the box).


I have code that can read lists, campaigns etc that I got from these forums.  This code works so I am including it in my simple example here.


I am getting a "The remote server returned an error: (400) Bad Request" WebException when trying to create a contact (if you run console app let it spit out XML, then hit enter and you will see error).  Here is my test console app (ALLCAPS means I have replace actual values):


 


class Program
{
static void Main(string[] args)
{
string APIKey = "MYGUIDKEY";
string myUsername = "MYUSERNAME";
string myPassword = "MYPASSWORD";
string myString = getContactLists(APIKey, myUsername, myPassword);
Console.WriteLine(myString);
Console.ReadLine();

string myString2 = createContact(APIKey, myUsername, myPassword);
Console.WriteLine(myString2);
Console.ReadLine();
}

 


public static string getContactLists(string APIKey, string myUsername, string myPassword)
{
CredentialCache LoginCredentials = new CredentialCache();

LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + myUsername), "Basic"new NetworkCredential(APIKey + "%" + myUsername, myPassword));

WebRequest Request = WebRequest.Create("https://api.constantcontact.com/ws/customers/" + myUsername + "/lists");
Request.Credentials = LoginCredentials;

string XMLResponse;
try
{
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

StreamReader Reader = new StreamReader(Response.GetResponseStream()); // Read the entire XML response to a string string
XMLResponse = Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();
Reader.Close();

Response.Close();

return XMLResponse;
}

catch (WebException e)
{
return ("WebExecption: " + e.Status + "\n" + e.Message);
}

catch (Exception e)
{
return ("General Exception: " + e.Message);
}
}

public static string createContact(string APIKey, string myUsername, string myPassword)
{
CredentialCache LoginCredentials = new CredentialCache();

LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + myUsername), "Basic"new NetworkCredential(APIKey + "%" + myUsername, myPassword));

HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" + myUsername + "/contacts");
Request.UserAgent = ".NET Programmatic Constant Contact Post Example";
Request.Credentials = LoginCredentials;

Request.ContentType = "application/atom+xml";
Request.Method = "POST";

//supposedly the least amount of data you can create a contact with
//http://developer.constantcontact.com/doc/manageContacts#create_contact
StringBuilder XMLData = new StringBuilder();
XMLData.Append("\"<entry xmlns=\"http://www.w3.org/2005/Atom\">");
XMLData.Append("<title type=\"text\"> </title>");
XMLData.Append("<updated>2008-07-23T14:21:06.407Z</updated>");
XMLData.Append("<author></author>");
XMLData.Append("<id>data:,none</id>");
XMLData.Append("<summary type=\"text\">Contact</summary>");
XMLData.Append("<content type=\"application/vnd.ctct+xml\">");
XMLData.Append("<Contact xmlns=\"http://ws.constantcontact.com/ns/1.0/\">");
XMLData.Append("<EmailAddress>test_100@example.com</EmailAddress>");
XMLData.Append("<OptInSource>ACTION_BY_CONTACT</OptInSource>");
XMLData.Append("<ContactLists>");
XMLData.Append("<ContactList id=\"http://api.constantcontact.com/ws/customers/CONSTANTCONTACTCUSTOMERNAME/lists/1\" />");
XMLData.Append("</ContactLists>");
XMLData.Append("</Contact>");
XMLData.Append("</content>");
XMLData.Append("</entry>");
byte[] byteArray = Encoding.UTF8.GetBytes(XMLData.ToString());

try
{
Request.ContentLength = byteArray.Length;
Stream streamRequest = Request.GetRequestStream();
// Write the data to the stream.
streamRequest.Write(byteArray, 0, byteArray.Length);
streamRequest.Close();
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
StreamReader Reader = new StreamReader(Response.GetResponseStream());
String XMLResponse = Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();
Reader.Close();
Response.Close();
return XMLResponse;
}

catch (WebException e)
{
return ("WebExecption: " + e.Status + "\n" + e.Message);
}

catch (Exception e)
{
return ("General Exception: " + e.Message);
}
}

}


 


 

2 REPLIES 2
bsoder
Employee

Hello,



I tested your code out on my end and I noticed that I believe the problem is.



In the line: XMLData.Append("\"<entry xmlns=\"http://www.w3.org/2005/Atom\">");, you have an additional \" at the beginning of the XML. If you remove that it works.




If you have any questions please let me know.



Regards,

Benjamin Soder
NOC Analyst
Constant Contact
mtrtm
Occasional Participant

Ben,


Thanks for the reply - that worked.  Good eyes! Not sure how I missed that :/

Developer Portal

View API documentation, code samples, get your API key.

Visit Page