cancel
Showing results for 
Search instead for 
Did you mean: 

XML API ASP.net 400 Bad Request

Highlighted
Occasional Participant

XML API ASP.net 400 Bad Request

I feel like I'm getting close. I've gotten some code from various sources and thus far am able to verify that I can successfully log in and as far as I can tell have the correct list id. I got that from https://constantcontact.mashery.com/io-docs. Below is my code, hoping someone can see what may be causing the error. Been working on it for a few hours, was hoping it would be simpler than this. Any assistance, ideas, active links would be appreciated. I'm translating everything to VB.net from C#

 

Try
                Dim xml As String
                xml = "<?xml version='1.0' encoding='utf-8' ?>" & _
                "<entry xmlns='http://www.w3.org/2005/Atom'>" & _
                    "<title type='text'></title>" & _
                    "<updated>2008-07-23T14:21:06.407Z</updated>" & _
                    "<author></author>" & _
                    "<id>data&colon;,none</id>" & _
                    "<summary type='text'>Contact</summary>" & _
                    "<content type='application/vnd.ctct+xml'>" & _
                        "<Contact xmlns='http://ws.constantcontact.com/ns/1.0/'>" & _
                            "<EmailAddress>" & Me.txtEmail.Text.Trim & "</EmailAddress>" & _
                            "<OptInSource>ACTION_BY_VISITOR</OptInSource>" & _
                            "<ContactLists>" & _
                                "<ContactList id='http://api.constantcontact.com/ws/customers/[Username]/lists/[listid]'></ContactLists>" & _
                        "</Contact>" & _
                    "</content>" & _
                "</entry>"

                ' Set up the request
                Dim request As HttpWebRequest
                request = CType(WebRequest.Create("https://api.constantcontact.com/ws/customers/[Username]/contacts"), HttpWebRequest)
                Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(xml.ToString())
                request.Method = "POST"
                request.ContentType = "application/atom+xml"
                request.ContentLength = byteData.Length
                request.Credentials = New NetworkCredential("[API Key]%[Username]", "[Password]")

                ' Send the request
                Dim postStream As Stream
                postStream = request.GetRequestStream()
                postStream.Write(byteData, 0, byteData.Length)

                Dim response As HttpWebResponse
                response = CType(request.GetResponse(), HttpWebResponse)
            Catch ex As WebException
End Try
5 REPLIES 5
CTCT Employee

Re: XML API ASP.net 400 Bad Request

Hi Steve,

 

I also believe you're close here. In using our XML API, you're using our older API. I'd generally recommend using our V2 (JSON) API because it will be around longer, but if you choose to keep going this way, the issue may be very small.

 

Instead of "ACTION_BY_VISITOR," you need to us "ACTION_BY_CONTACT." The name of this parameter varies based on the version of the API you're using, so you probably saw the visitor version when you looked at V2 documentation, or a forum post about the V2 API.

 

I hope that helps! If you still get a 400 error, I would vary the email address you're using to make sure you're adding an email address that's never been in the account. Also make sure your variables like [Username] inside the XML are getting filled in properly, if you haven't already checked.

 

Best Regards,

Shannon Wallace

Partner API Support Engineer

Occasional Participant

Re: XML API ASP.net 400 Bad Request

Thanks but that still didn't work, this is frustrating. Not opposed to using JSON but trying to find any examples that actually work is difficult. The XML was the only one I fount that was complete and had active links. Most Google searches turn up results that are 5+ years old and contain dead links within them. It'd be nice if, like other API's, there was thorough documentation that outlined exact steps with examples, options and parameters. It seems the API's for Contstant Contact are catered towards serious developers with in depth knolwedge of JSON, XML, etc.

 

Are there any links that are active, regarding JSON, for ASP.net, and complete (as in working examples that I don't need to have multiple windows open to deipher what is being stated or that will lead me to more questions)?

CTCT Employee

Re: XML API ASP.net 400 Bad Request

Hi Steve,

 

This is our V1 API documentation. It may have been what you were looking for. This is the page that shows how to add a new contact, and this is the page that has all of the contact parameters and fields. Perhaps your point was that you have to go to too many pages to find all that, and your point is well-taken. However, we have come quite a way with our V2 documentation, but it certainly is catered towards seasoned developers.

 

If those pages don't help you solve your problems, I'd be happy to take a more in-depth look at your XML. The entire XML with the values filled in would be best, and you can send that to webservices@constantcontact.com for help.

 

Thanks!

 

Best Regards,

Shannon Wallace

Partner API Support Engineer

 

 

Occasional Participant

Re: XML API ASP.net 400 Bad Request

Thanks, that's the info I came across along with some others to generate what I have. I'm searching for the v2 documentation now. If you have some preferred/recommended links to get me started please pass them along.

CTCT Employee

Re: XML API ASP.net 400 Bad Request

Hi Steve,

 

Absolutely. You may want to use our .NET SDK and convert the C# addContact example file into VB. The raw documentation pages for adding contacts are here and here. If you'd be interested in using a simple post URL, especially if your primary and only goal is to add contacts individually to an account, this is an article I wrote about how to use it.

 

Best Regards,

Shannon Wallace

Partner API Support Engineer