create campaign using API says 400 bad request

Occasional Participant

create campaign using API says 400 bad request

we have a website (programming language c#) in which we are integrating Constant Contact. we are creating campaign using API but getting "Bad Request" response. we followed to make xml. we cross verified each single xml node and it seems me perfect. However still it gives "Bad Request". Please help on this. below is our xml that we are posting.


<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns="">
<link href="/ws/customers/username/campaigns" rel="edit" />
<title type="text">API Test Email</title>
<name>Constant Contact</name>
<content type="application/vnd.ctct+xml">
<Campaign xmlns="" id="">
<Name>API Test Email</Name>
<Subject>Enter email subject here</Subject>
<PermissionReminderText>You're receiving this email because of your relationship with ctct.</PermissionReminderText>
<OrganizationAddress1>123 wsw st</OrganizationAddress1>
<EmailContent>Test Email</EmailContent>
<EmailTextContent>Test Email</EmailTextContent>
<ContactList id="">
<link xmlns="" href="/ws/customers/username/lists/1" rel="self" />
<Email id="">
<link xmlns="" href="/ws/customers/username/settings/emailaddresses/1" rel="self" />
<Email id="">
<link xmlns="" href="/ws/customers/username/settings/emailaddresses/1" rel="self" />
<title type="text">Campaigns for customer: username</title>
<link href="campaigns" /><link href="campaigns" rel="self" />





The problems I see are related to your <EmailContent> and <EmailTextContent> nodes.  Neither value is a valid value and should be returning a 400 error telling you that they are not valid.


<EmailContent> must be a well formed, valid HTML document that is HTML Encoded prior to adding it to your XML document per the XML spec.


<EmailTextContent> must be wrapped in <Text></Text> nodes per our documentation and requirements.  This content must then also be HTML encoded prior to adding to the XML document per the XML spec.


You should be able to receive hte full response message including the detailed errors in C# by catching a WebException in your code and pulling the HttpWebResponse out of the exception.  You'd then just need to use the GetResponseStream() method of the HttpWebResponse to get a Stream object with our full response text.  This response would tell you the specific nodes or content that are generating errors and is very helpful in working with a RESTful API.  For example, you could do something like the following:


catch (WebException ex){

String ErrorResponse = new StreamReader(((HttpWebResponse)ex.Response).GetResponseStream()).ReadToEnd();


Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant

Hello Dave,


I have modified the xml as you suggested but still getting 400 error.


below is the node with text I changed:


<EmailContent>&lt;html lang=&quot;en&quot; xml:lang=&quot;en&quot; xmlns=&quot;; xmlns:cctd=&quot;;&gt;&lt;body&gt;test message&lt;/body&gt;&lt;/html&gt;</EmailContent>


I also used the c# code you sent me to know the exact node error message but it gives me below message description and I am unable to understand for which node this error is.


Fault Name: HttpRequestReceiveError\nError Type: Default\nDescription: Http request received failed\nRoot Cause Code: -19015\nRoot Cause : HTTP Transport: Syntax error while parsing\nBinding State: CLIENT_CONNECTION_ESTABLISHED\nService: null\nEndpoint: null\nOperation (Client):\n\n


just for reference here is my c# processing code for create campaign API:


CredentialCache LoginCredentials = new CredentialCache();
LoginCredentials.Add(new Uri(BaseUri + sUsername), "Basic", new NetworkCredential(sAPIKey + "%" + sUsername, sPassword));
WebRequest Request = WebRequest.Create(ContactURI);
Request.Method = "POST";
Request.ContentType = "application / atom + xml";
Request.Credentials = LoginCredentials;

string XMLData = CreateCampaignXml(); // this formed campaign xml string

byte[] byteArray = UTF8Encoding.UTF8.GetBytes(XMLData);

Request.ContentLength = byteArray.Length;
string XMLResponse = "Bytes to send:" + byteArray.Length;

Stream streamRequest = Request.GetRequestStream();
streamRequest.Write(byteArray, 0, byteArray.Length);

HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

StreamReader Reader = new StreamReader(Response.GetResponseStream());
XMLResponse += Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();

catch (WebException ex)
string gg = new StreamReader(((HttpWebResponse)ex.Response).GetResponseStream()).ReadToEnd();
catch (Exception err)
string gg = err.Message;




That error message is not being returned from Constant Contact, that's typically a connection issue error that is returned by a transport layer between you and Constant Contact.


Are you able to reproduce this error message through a debug environment such as a RESTClient

Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant

Hello Dave,


finally I got the solution. it was due to:


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


I should write it as:


Request.ContentType = "application/atom+xml"; // NO SPACE



Developer Portal

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

Visit Page