The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

create campaign using API says 400 bad request

Highlighted
Occasional Participant

create campaign using API says 400 bad request

we have a website (programming language asp.net c#) in which we are integrating Constant Contact. we are creating campaign using API but getting "Bad Request" response. we followed http://community.constantcontact.com/t5/Documentation/Creating-a-Campaign/ba-p/25047 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="http://www.w3.org/2005/Atom">
<link href="/ws/customers/username/campaigns" rel="edit" />
<id>http://api.constantcontact.com/ws/customers/username/campaigns</id>
<title type="text">API Test Email</title>
<updated>2009-10-19T18:34:53.105Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<Campaign xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/username/campaigns/1100546096289">
<Name>API Test Email</Name>
<Status>Draft</Status>
<Date>2009-10-19T18:34:53.105Z</Date>
<Subject>Enter email subject here</Subject>
<FromName>me@me.com</FromName>
<ViewAsWebpage>NO</ViewAsWebpage>
<ViewAsWebpageLinkText></ViewAsWebpageLinkText>
<ViewAsWebpageText></ViewAsWebpageText>
<PermissionReminder>YES</PermissionReminder>
<PermissionReminderText>You're receiving this email because of your relationship with ctct.</PermissionReminderText>
<GreetingSalutation>Dear</GreetingSalutation>
<GreetingName>FirstName</GreetingName>
<GreetingString>Greetings!</GreetingString>
<OrganizationName>ctct</OrganizationName>
<OrganizationAddress1>123 wsw st</OrganizationAddress1>
<OrganizationAddress2></OrganizationAddress2>
<OrganizationAddress3></OrganizationAddress3>
<OrganizationCity>Ashland</OrganizationCity>
<OrganizationState>MA</OrganizationState>
<OrganizationInternationalState></OrganizationInternationalState>
<OrganizationCountry>us</OrganizationCountry>
<OrganizationPostalCode>32423</OrganizationPostalCode>
<IncludeForwardEmail>NO</IncludeForwardEmail>
<ForwardEmailLinkText></ForwardEmailLinkText>
<IncludeSubscribeLink>NO</IncludeSubscribeLink>
<SubscribeLinkText></SubscribeLinkText>
<EmailContentFormat>HTML</EmailContentFormat>
<EmailContent>Test Email</EmailContent>
<EmailTextContent>Test Email</EmailTextContent>
<StyleSheet></StyleSheet>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/username/lists/1">
<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/username/lists/1" rel="self" />
</ContactList>
</ContactLists>
<FromEmail>
<Email id="http://api.constantcontact.com/ws/customers/username/settings/emailaddresses/1">
<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/username/settings/emailaddresses/1" rel="self" />
</Email>
<EmailAddress>me@me.com</EmailAddress>
</FromEmail>
<ReplyToEmail>
<Email id="http://api.constantcontact.com/ws/customers/username/settings/emailaddresses/1">
<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/username/settings/emailaddresses/1" rel="self" />
</Email>
<EmailAddress>me@me.com</EmailAddress>
</ReplyToEmail>
</Campaign>
</content>
<source>
<id>http://api.constantcontact.com/ws/customers/username/campaigns</id>
<title type="text">Campaigns for customer: username</title>
<link href="campaigns" /><link href="campaigns" rel="self" />
<author>
<name>username</name>
</author>
<updated>2009-10-19T19:36:12.622Z</updated>
</source>
</entry>

 

thanks

 


4 REPLIES 4
Highlighted
Moderator

Re: create campaign using API says 400 bad request

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
Highlighted
Occasional Participant

Re: create campaign using API says 400 bad request

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;http://www.w3.org/1999/xhtml&quot; xmlns:cctd=&quot;http://www.constantcontact.com/cctd&quot;&gt;&lt;body&gt;test message&lt;/body&gt;&lt;/html&gt;</EmailContent>
<EmailTextContent>&lt;Text&gt;test&lt;/Text&gt;</EmailTextContent>

 

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);

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

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

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

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

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

 

thanks

 

Highlighted
Moderator

Re: create campaign using API says 400 bad request

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
Highlighted
Occasional Participant

Re: create campaign using API says 400 bad request

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

 

thanks


Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured