Reply
Occasional Contributor
HipChick
Posts: 17
Registered: ‎11-01-2008
Accepted Solution

Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

Hi All,

 I am getting the Error : 400 bad request , while creating a sample campaign using Java Client of CTCTRest API. Is it some thing emailaddress settings needs to be done or XML request problem ?

 XML Used :

----------------

<?xml version='1.0'encoding='UTF-8'?>

<entry xmlns="http://www.w3.org/2005/Atom">

<link href="/ws/customers/shipcarsnow1/campaigns"rel="edit"/>

<id>http://api.constantcontact.com/ws/customers/shipcarsnow1/campaigns</id>

<title type="text">ShipCarsNow Sample Campaign</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/shipcarsnow1/1111122222333">

<Name>ShipCarsNow Sample Campaign</Name>

<Status>Draft</Status>

<Date>2009-10-19T18:34:53.105Z</Date>

<Subject>We'd like your feedback about ShipCarsNow</Subject>

<FromName>ShipCarsNow Test</FromName>

<ViewAsWebpage>YES</ViewAsWebpage>

<ViewAsWebpageLinkText>Click here</ViewAsWebpageLinkText>

<ViewAsWebpageText>Having trouble viewing this email?</ViewAsWebpageText>

<PermissionReminder>NO</PermissionReminder>

<PermissionReminderText></PermissionReminderText>

<GreetingSalutation>Dear</GreetingSalutation>

<GreetingName>FirstName</GreetingName>

<GreetingString>Greetings!</GreetingString>

<OrganizationName>ShipCarsNow</OrganizationName>

<OrganizationAddress1>Mail Stop 0430</OrganizationAddress1>

<OrganizationAddress2></OrganizationAddress2>

<OrganizationAddress3></OrganizationAddress3>

<OrganizationCity>Omaha</OrganizationCity>

<OrganizationState>NE</OrganizationState>

<OrganizationInternationalState></OrganizationInternationalState>

<OrganizationCountry>US</OrganizationCountry>

<OrganizationPostalCode>68102</OrganizationPostalCode>

<IncludeForwardEmail>NO</IncludeForwardEmail>

<ForwardEmailLinkText></ForwardEmailLinkText>

<IncludeSubscribeLink>NO</IncludeSubscribeLink>

<SubscribeLinkText></SubscribeLinkText>

<EmailContentFormat>HTML</EmailContentFormat>

<EmailContent>&lt;html lang="en" xml:lang="en"

xmlns="http://www.w3.org/1999/xhtml"

xmlns:cctd="http://www.constantcontact.com/cctd">

&lt;body>&lt;CopyRight>Copyright (c) 1996-2009 Constant Contact.

All rights reserved. Except as permitted under a

separate

written agreement with Constant Contact, neither the Constant Contact

software, nor any content that appears on any

Constant Contact site,

including but not limited to, web pages, newsletters, or templates may be

reproduced, republished, repurposed, or

distributed without the

prior written permission of Constant Contact. For inquiries regarding

reproduction or distribution of any Constant

Contact material, please

contact rdevapa@upcontractor.up.com.&lt;/CopyRight>

&lt;OpenTracking/>

&lt;!-- Do NOT delete previous line if you want to get statistics on the

number of opened emails -->

&lt;CustomBlock name="letter.intro" title="Personalization">

&lt;Greeting/>

&lt;/CustomBlock>

&lt;/body>

&lt;/html>

</EmailContent>

<EmailTextContent>&lt;Text>This is the text version.&lt;/Text></EmailTextContent>

<StyleSheet></StyleSheet>

<ContactLists>

<ContactList id="http://api.constantcontact.com/ws/customers/shipcarsnow1/lists/170">

<link xmlns="http://www.w3.org/2005/Atom"  href="/ws/customers/shipcarsnow1/lists/170" rel="self"/>

</ContactList></ContactLists>

<FromEmail>

<Email id="http://api.constantcontact.com/ws/customers/shipcarsnow1/settings/emailaddresses/170">

<link xmlns="http://www.w3.org/2005/Atom"  href="/ws/customers/shipcarsnow1/settings/emailaddresses/170" rel="self"/>

</Email>

<EmailAddress>rdevapa@upcontractor.up.com</EmailAddress>

</FromEmail>

<ReplyToEmail>

<Email id="http://api.constantcontact.com/ws/customers/shipcarsnow1/settings/emailaddresses/170">

<link xmlns="http://www.w3.org/2005/Atom"  href="/ws/customers/shipcarsnow1/settings/emailaddresses/170"  rel="self"/>

</Email>

<EmailAddress>rdevapa@upcontractor.up.com</EmailAddress>

</ReplyToEmail>

</Campaign>

</content>

<source>

<id>http://api.constantcontact.com/ws/customers/shipcarsnow1/campaigns</id>

<title type="text">Campaigns for customer: Shipcarsnow1</title>

<link href="campaigns"/>

<link href="campaigns"rel="self"/>

<author>

<name>shipcarsnow1</name>

</author>

<updated>2009-10-19T18:34:53.105Z</updated>

</source>

</entry>

================================ 

JAVA Code Snippet :

================

HttpPost httppost =newHttpPost("https://api.constantcontact.com/ws/customers/shipcarsnow1/campaigns");

httppost.addHeader("Content-Type", "application/atom+xml");

ByteArrayEntity entity = newByteArrayEntity(xmlRequest.getBytes());

httppost.setEntity(entity);

HttpResponse response = getAuthenticatedConnection().execute(httppost);

System.out.println("Response status :"+response.getStatusLine());

InputStreamReader r = newInputStreamReader(response.getEntity().getContent());

=============

 

public DefaultHttpClient getAuthenticatedConnection() throwsClientProtocolException, IOException {

SchemeRegistry schemeRegistry = newSchemeRegistry();

schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

org.apache.http.params.HttpParams params = newBasicHttpParams();

ThreadSafeClientConnManager cm = newThreadSafeClientConnManager(params, schemeRegistry);

 

httpclient = newDefaultHttpClient(cm, params);

 

String loginUsername = (new StringBuilder(String.valueOf(API_KEY))).append("%").append(username).toString();

 

httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(loginUsername, password));

StringBuilder stringBuilder = new StringBuilder("https://api.constantcontact.com/ws/customers/");

stringBuilder.append(username).append("/");

System.out.println(stringBuilder.toString());

InputStream stream = doGetRequest(stringBuilder.toString());

if(stream != null){

System.out.println("Autt User");

}

else{

System.out.println("Not Autt User");

}

returnhttpclient;

}

Richard_M
Posts: 30
Topics: 0
Kudos: 1
Solutions: 3
Registered: ‎02-15-2011

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

[ Edited ]

Hi,

 

On lines 3 and 90 you are missing a space between rel and the quote before it. For example on Line 3, this code

 

<link href="/ws/customers/shipcarsnow1/campaigns"rel="edit"/>

should be this:

 

<link href="/ws/customers/shipcarsnow1/campaigns" rel="edit"/>

Likewise, on Line 90, this:

 

<link href="campaigns"rel="self"/>

should be this.

 

<link href="campaigns" rel="self"/>

Regards,

Richard Maxwell
Support Engineer, Constant Contact
Occasional Contributor
HipChick
Posts: 17
Registered: ‎11-01-2008

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

Richard,

Its missed while copy paste the xml from Eclipse, space is there in XML, did  u find any other problem with xml ?

 

My doubt is do we need to do any thing with settings and what is the significance of the folloowing line ?

I have created a sample list 170, added some emilids to that list , apart from that I haven't done any settings/emailaddress like : "/ws/customers/shipcarsnow1/settings/emailaddresses/170

 

 

<ContactLists>

<ContactList id="http://api.constantcontact.com/ws/customers/shipcarsnow1/lists/170">

<link xmlns="http://www.w3.org/2005/Atom"  href="/ws/customers/shipcarsnow1/lists/170"  rel="self"/>

</ContactList>

</ContactLists>

<FromEmail>

<Email id="http://api.constantcontact.com/ws/customers/shipcarsnow1/settings/emailaddresses/170">

<link xmlns=http://www.w3.org/2005/Atomhref="/ws/customers/shipcarsnow1/settings/emailaddresses/170" rel="self"/>

</Email>

<EmailAddress>rdevapa@upcontractor.up.com</EmailAddress>

</FromEmail>

 

Richard_M
Posts: 30
Topics: 0
Kudos: 1
Solutions: 3
Registered: ‎02-15-2011

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

[ Edited ]

The rdevapa@upcontractor.up.com address is not a verified address in your account. Our system will return a 400 error if you try to specify a from address that is not verified. You can get the address numbers and the address of each verified contact from the following URL:

 

https://api.constantcontact.com/ws/customers/shipcarsnow1/settings/emailaddresses

 

Just visit that URL and enter in your API key, followed by a percent sign and your user name in the user name box. Then, enter your password in the password box. Then, you can view the XML by viewing the source for the page. The correct number of the contacts is shown in the id node.

 

Also, on Line 11 of your XML, there is an extra space. This:

 

<Campaign xmlns="http://ws.constantcontact.com/ns/1.0/ "

should be this:

 

<Campaign xmlns="http://ws.constantcontact.com/ns/1.0/"

I was able to successfully create a campaign using your XML after correcting the above issues

Richard Maxwell
Support Engineer, Constant Contact
Occasional Contributor
HipChick
Posts: 17
Registered: ‎11-01-2008

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

I have corrected those email still getting bad request : here is XML copied from Test Editor:

 

<?xml version="1.0" encoding="UTF-8"?>

<entry xmlns="http://www.w3.org/2005/Atom">

<link href="/ws/customers/shipcarsnow1/campaigns" rel="edit"/>

<id>http://api.constantcontact.com/ws/customers/shipcarsnow1/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/shipcarsnow1/campaigns/1111122233">

<Name>API Test Email</Name>

<Status>Draft</Status>

<Date>2009-10-19T18:34:53.105Z</Date>

<Subject>ShipCarsNow Test</Subject>

<FromName>ShipCarsNow Test</FromName>

<ViewAsWebpage>NO</ViewAsWebpage>

<ViewAsWebpageLinkText/>

<ViewAsWebpageText/>

<PermissionReminder>YES</PermissionReminder>

<PermissionReminderText>

You're receiving this email because of your relationship with ctct.

Please

<ConfirmOptin>

<a style="color:#0000ff;">confirm</a>

</ConfirmOptin>

your continued interest in receiving email from us.

</PermissionReminderText>

<GreetingSalutation>Dear</GreetingSalutation>

<GreetingName>FirstName</GreetingName>

<GreetingString>Greetings!</GreetingString>

<OrganizationName>ShipCarsnow</OrganizationName>

<OrganizationAddress1>123 wsw st</OrganizationAddress1>

<OrganizationAddress2/>

<OrganizationAddress3/>

<OrganizationCity>OMAHA</OrganizationCity>

<OrganizationState>NE</OrganizationState>

<OrganizationInternationalState/>

<OrganizationCountry>US</OrganizationCountry>

<OrganizationPostalCode>32423</OrganizationPostalCode>

<IncludeForwardEmail>NO</IncludeForwardEmail>

<ForwardEmailLinkText/>

<IncludeSubscribeLink>NO</IncludeSubscribeLink>

<SubscribeLinkText/>

<EmailContentFormat>HTML</EmailContentFormat>

<EmailContent>

<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"

xmlns:cctd="http://www.constantcontact.com/cctd">

<body>

<CopyRight>Copyright (c) 1996-2009 Constant Contact. All rights

reserved. Except as permitted under a separate written agreement

with Constant Contact, neither the Constant Contact software, nor

any content that appears on any Constant Contact site, including

but not limited to, web pages, newsletters, or templates may be

reproduced, republished, repurposed, or distributed without the

prior written permission of Constant Contact. For inquiries

regarding reproduction or distribution of any Constant Contact

material, please contact slboucher@shipcarsnow.com.

</CopyRight>

<OpenTracking/>

<!-- Do NOT delete previous line if you want to get statistics on the

number of opened emails -->

<CustomBlock name="letter.intro" title="Personalization">

<Greeting/>

</CustomBlock>

</body>

</html>

</EmailContent>

<EmailTextContent>

<Text>This is the text version.</Text>

</EmailTextContent>

<StyleSheet/>

<ContactLists>

<ContactList

id="http://api.constantcontact.com/ws/customers/shipcarsnow1/lists/170">

<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/shipcarsnow1/lists/170"

rel="self"/>

</ContactList>

</ContactLists>

<FromEmail>

<Email

id="http://api.constantcontact.com/ws/customers/shipcarsnow1/settings/emailaddresses/8">

<link xmlns="http://www.w3.org/2005/Atom"

href="/ws/customers/shipcarsnow1/settings/emailaddresses/8" rel="self"/>

</Email>

<EmailAddress>slboucher@shipcarsnow.com</EmailAddress>

</FromEmail>

<ReplyToEmail>

<Email

id="http://api.constantcontact.com/ws/customers/shipcarsnow1/settings/emailaddresses/8">

<link xmlns="http://www.w3.org/2005/Atom"

href="/ws/customers/shipcarsnow1/settings/emailaddresses/8" rel="self"/>

</Email>

<EmailAddress>slboucher@shipcarsnow.com</EmailAddress>

</ReplyToEmail>

</Campaign>

</content>

<source>

<id>http://api.constantcontact.com/ws/customers/shipcarsnow1/campaigns

</id>

<title type="text">Campaigns for customer: shipcarsnow1</title>

<link href="campaigns"/>

<link href="campaigns" rel="self"/>

<author>

<name>shipcarsnow1</name>

</author>

<updated>2009-10-19T19:36:12.622Z</updated>

</source>

</entry>

 

 

 

Richard_M
Posts: 30
Topics: 0
Kudos: 1
Solutions: 3
Registered: ‎02-15-2011

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

The XML content posted above is different from the original XML. I found these errors in the new XML:

 

  1. The </id> closing tag needs to be on the same line as <id>.
  2. The left-angle brackets inside PermissionReminderText, EmailContent, and EmailTextContent need to be converted to&lt; enttites.

So, here is how the new XML should look:

 

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<link href="/ws/customers/shipcarsnow1/campaigns" rel="edit"/>
<id>http://api.constantcontact.com/ws/customers/shipcarsnow1/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/shipcarsnow1/campaigns/1111122233">
<Name>API Test Email</Name>
<Status>Draft</Status>
<Date>2009-10-19T18:34:53.105Z</Date>
<Subject>ShipCarsNow Test</Subject>
<FromName>ShipCarsNow Test</FromName>
<ViewAsWebpage>NO</ViewAsWebpage>
<ViewAsWebpageLinkText/>
<ViewAsWebpageText/>
<PermissionReminder>YES</PermissionReminder>
<PermissionReminderText>
You're receiving this email because of your relationship with ctct.
Please
&lt;ConfirmOptin>
&lt;a style="color:#0000ff;">confirm&lt;/a>
&lt;/ConfirmOptin>
your continued interest in receiving email from us.
&lt;/PermissionReminderText>
<GreetingSalutation>Dear</GreetingSalutation>
<GreetingName>FirstName</GreetingName>
<GreetingString>Greetings!</GreetingString>
<OrganizationName>ShipCarsnow</OrganizationName>
<OrganizationAddress1>123 wsw st</OrganizationAddress1>
<OrganizationAddress2/>
<OrganizationAddress3/>
<OrganizationCity>OMAHA</OrganizationCity>
<OrganizationState>NE</OrganizationState>
<OrganizationInternationalState/>
<OrganizationCountry>US</OrganizationCountry>
<OrganizationPostalCode>32423</OrganizationPostalCode>
<IncludeForwardEmail>NO</IncludeForwardEmail>
<ForwardEmailLinkText/>
<IncludeSubscribeLink>NO</IncludeSubscribeLink>
<SubscribeLinkText/>
<EmailContentFormat>HTML</EmailContentFormat>
<EmailContent>
&lt;html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:cctd="http://www.constantcontact.com/cctd">
&lt;body>
&lt;CopyRight>Copyright (c) 1996-2009 Constant Contact. All rights
reserved. Except as permitted under a separate written agreement
with Constant Contact, neither the Constant Contact software, nor
any content that appears on any Constant Contact site, including
but not limited to, web pages, newsletters, or templates may be
reproduced, republished, repurposed, or distributed without the
prior written permission of Constant Contact. For inquiries
regarding reproduction or distribution of any Constant Contact
material, please contact slboucher@shipcarsnow.com.
&lt;/CopyRight>
&lt;OpenTracking/>
&lt;!-- Do NOT delete previous line if you want to get statistics on the
number of opened emails -->
&lt;CustomBlock name="letter.intro" title="Personalization">
&lt;Greeting/>
&lt;/CustomBlock>
&lt;/body>
&lt;/html>
</EmailContent>
<EmailTextContent>
&lt;Text>This is the text version.&lt;/Text>
</EmailTextContent>
<StyleSheet/>
<ContactLists>
<ContactList
id="http://api.constantcontact.com/ws/customers/shipcarsnow1/lists/170">
<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/shipcarsnow1/lists/170"
rel="self"/>
</ContactList>
</ContactLists>
<FromEmail>
<Email
id="http://api.constantcontact.com/ws/customers/shipcarsnow1/settings/emailaddresses/8">
<link xmlns="http://www.w3.org/2005/Atom"
href="/ws/customers/shipcarsnow1/settings/emailaddresses/8" rel="self"/>
</Email>
<EmailAddress>slboucher@shipcarsnow.com</EmailAddress>
</FromEmail>
<ReplyToEmail>
<Email
id="http://api.constantcontact.com/ws/customers/shipcarsnow1/settings/emailaddresses/8">
<link xmlns="http://www.w3.org/2005/Atom"
href="/ws/customers/shipcarsnow1/settings/emailaddresses/8" rel="self"/>
</Email>
<EmailAddress>slboucher@shipcarsnow.com</EmailAddress>
</ReplyToEmail>
</Campaign>
</content>
<source>
<id>http://api.constantcontact.com/ws/customers/shipcarsnow1/campaigns
</id>
<title type="text">Campaigns for customer: shipcarsnow1</title>
<link href="campaigns"/>
<link href="campaigns" rel="self"/>
<author>
<name>shipcarsnow1</name>
</author>
<updated>2009-10-19T19:36:12.622Z</updated>
</source>
</entry>
Richard Maxwell
Support Engineer, Constant Contact
Occasional Contributor
HipChick
Posts: 17
Registered: ‎11-01-2008

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

[ Edited ]

Richard,

 

I tried with the XML provided by you still am getting the Bad Request error, let me know if its possible to send logs contains any info about my request based, latest time i runned the program is 9:30 AM CST April 23rd 2012.

 

Also the list which we created has do required any permisiion ? meaning is the following part of xml needs any permission etc and let us do our API_KEY has permission to create campaign ro not our API_KEY :

 "1af18f30-b9f3-437d-b37d-8d0bd10f92b5" ?

 

<ContactLists>

<ContactList id="http://api.constantcontact.com/ws/customers/shipca​rsnow1/lists/170">

<link xmlns="http://www.w3.org/2005/Atom"  href="/ws/customers/shipcarsnow1/lists/170" rel="self"/>

</ContactList>

</ContactLists>

 

 

Richard_M
Posts: 30
Topics: 0
Kudos: 1
Solutions: 3
Registered: ‎02-15-2011

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

Hi,

 

That API key is valid. Usually when you do not have permission to access a certain resource, you will get a 401 error, instead of a 400 one. I would double-check the list number. If you visit https://api.constantcontact.com/ws/customers/shipca​rsnow1/lists and view the source of the page, you will see the list numbers inside each ID node. I would also try inputting your XML into the REST client: http://community.constantcontact.com/t5/Documentation/How-To-Use-RESTClient/ba-p/24915. The REST client will give you details about exactly what kind of 400 error you are getting in the Body tab of the bottom pane in the application. It is also a good way to determine if the issue is the XML or the server-side code that is causing the issue.

Richard Maxwell
Support Engineer, Constant Contact
Occasional Contributor
HipChick
Posts: 17
Registered: ‎11-01-2008

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

Richard,

 

thanks for giving tip about the RestClient, it worked fine , when I extratced existing campign's xml and modified the same to create new campaign was successful.

 

I want to know is ShipCarsNow account has permision to Schduling and Sending  Compaing using out API_KEY ?

Richard_M
Posts: 30
Topics: 0
Kudos: 1
Solutions: 3
Registered: ‎02-15-2011

Re: Creating Campaign using JAVA giving : Response status :HTTP/1.1 400 Bad Request

Hi,

 

Your API key currently does not have permission to schedule and send. I just e-mailed you some questions about it. Once we get those answered, I can enable your key for scheduling and send you the scheduling documentation.

 

Thanks,

Richard Maxwell
Support Engineer, Constant Contact