Trying to add a new contact list, just a list name. didn't find details on this in docs. We get a Media not supported error when trying this code. Can you offer help?
Public Shared Sub AddNewContactList(ByVal sListName As String) 'Setup your variables Dim ctu As String = GetCTU() Dim ctp As String = GetCTP() Dim sUri As String = "https://api.constantcontact.com/ws/customers/" & ctu & "/activities" Dim sListUri As String = sListName Dim sAPIKey As String = "CONSTANT_CONTACT_API_KEY" Try 'Setup an HttpWebRequest to send the data Dim address As New Uri(sUri) Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest) request.Credentials = New NetworkCredential((sAPIKey & "%" & ctu), ctp) request.Method = "POST" request.ContentType = "application/vnd.ctct+xml" 'Build an encoded string of the data to pass to Constant Contact Dim data As New StringBuilder() 'data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8)) data.Append("&data=" + HttpUtility.UrlEncode(("OptInDefault,Name" & Chr(10)), Encoding.UTF8)) data.Append(HttpUtility.UrlEncode(("true," & sListName), Encoding.UTF8)) 'data.Append("&lists=" + HttpUtility.UrlEncode(sListUri)) 'The "guts" of the code to execute the request and return a response Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(data.ToString()) Dim st As String = String.Empty request.ContentLength = byteData.Length Using postStream As Stream = request.GetRequestStream() postStream.Write(byteData, 0, byteData.Length) End Using Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse) Dim reader As New StreamReader(response.GetResponseStream()) st = reader.ReadToEnd() End Using Catch ex As Exception HttpContext.Current.Response.Write(ex.Message) End Try End Sub
The problem is your content type, you are setting it to an invalid content type and thus correctly getting a 415 media not supported error. The correct content type for this is:
request.ContentType = "application/x-www-form-urlencoded"
Please see our documentation for creating bulk activity requests here for more information: http://community.constantcontact.com/t5/Documentation/Creating-an-Add-Contacts-Remove-Contacts-Activ...
David,
I appreciate the reply, but I think we are talking about two different things.
I'm trying to simply add the name of a new list. not contacts to a list.
Could you please check my previously posted code with that in-mind.
Thank you,
Sorry for the confusion there.
The Activities API is only used for importing/exporting bulk contact jobs. You are not able to create new lists through this API, it assumes the list(s) in question already exist and will provide an error if they do not.
To create a new List, you would need to use the Lists Collection API, documentation found here: http://community.constantcontact.com/t5/Documentation/Creating-a-New-List/ba-p/25069
This is one of our RESTful APIs that allows you to realtime create, update and delete lists. The List ID you receive back from this API is what you would use for the Activities API when creating new import/exports.
Hope this helps.
Dave,
Can you offer any suggestions on my code sample to move me forward. I'm not getting much from the suggested reading.
Thank you,
The best suggestion I can give is to leverage our already existing .NET sample. While it is written in C#, there are two simple options for using it in VB.NET.
Using either of these options you won't have to write any code that directly accesses our API. You can use objects, such as a List object, to create and manipulate resources through our API. You can find a link to both here: http://community.constantcontact.com/t5/Documentation/Code-Samples/ba-p/25019
If this won't work for you, you will need to create the XML needed to create a List from our documentation by hand. The XML you create would then end up being the content of your HttpWebRequest object. Here is a shell of code, taken from your post, to get you started. Please note, this does not create the XML content. You will need to do that with whatever data you want to submit.
Public Shared Sub AddNewContactList(ByVal sListName As String)
'Setup your variables
Dim ctu As String = GetCTU()
Dim ctp As String = GetCTP()
Dim sUri As String = "https://api.constantcontact.com/ws/customers/" & ctu & "/lists"
Dim sListUri As String = sListName
Dim sAPIKey As String = "CONSTANT_CONTACT_API_KEY"
Try
'Setup an HttpWebRequest to send the data
Dim address As New Uri(sUri)
Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)
request.Credentials = New NetworkCredential((sAPIKey & "%" & ctu), ctp)
request.Method = "POST"
request.ContentType = "application/atom+xml"
'Build an encoded string of the data to pass to Constant Contact
Dim data As New StringBuilder()
data.Append("<entry xmlns=""http://www.w3.org/2005/Atom"">")
data.Append("<id>data:,</id>")
data.Append("<title />")
data.Append("<author />")
data.Append("<updated>2008-04-16</updated>")
data.Append("<content type=""application/vnd.ctct+xml"">")
data.Append("<OptInDefault>false</OptInDefault>")
data.Append("<Name>" & sListName & "</Name>")
data.Append("<SortOrder>99</SortOrder>")
data.Append("</ContactList>")
data.Append("</content>")
data.Append("</entry>")
'The "guts" of the code to execute the request and return a response
Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(data.ToString())
Dim st As String = String.Empty
request.ContentLength = byteData.Length
Using postStream As Stream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)
End Using
Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(response.GetResponseStream())
st = reader.ReadToEnd()
End Using
Catch ex As Exception
HttpContext.Current.Response.Write(ex.Message)
End Try
End Sub
Dave,
Sorry to be a bother with this, but I looked through all the c#utilities in downloaded zip file and there is nothing that offers any specific help for what I need.
Just to be sure that we are on the same page. What I need to do from the api is the same as if I was logged into our ct account and clicked Contacts, then clicked the "Create New" link under Lists, and enter a new list name, then clicked save to add that single new list name. No contacts, just a list name.
Any suggestions?
The code snippet I gave above in VB should do exactly that. Did that code not work for you?
Dave,
No, it errors out with the same error "The remote server returned an error: (415) Unsupported Media Type."
Was a typo on my part, the content type for the request was set incorrectly still. Please try again with the changes I just put in.
Dave,
I made the change top the request.contenttype and now I receive the error: :The remote server returned an error: (400) Bad Request. "
You should be able to use code similar to this to read the full response to that error. It will tell you what the cause of the 400 is, which indicates that some of the XML content you sent was not valid. If you can get the full response, we can help direct you to fixing the XML.
Using ex.response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(response.GetResponseStream())
String st = reader.ReadToEnd()
Dave,
Because of the error we are not having any luck in getting the full response back as you suggested. If the xml is being sent as it is in your example. do you know for sure that that example xml is valid? Or maybe you know how we can modify our response code to to return the full response or another way to see the full response.
In any case we appreciate any suggestions you can offer.
The code I mentioned must be put in the catch block. That way, once the HttpWebRequest throws the exception, you can get the full response through the exception being thrown.
I think it may be my fault for not noticing this earlier, but the correct way to catch this code is with a webexception, not a standard exception. You would want to change your catch to:
catch ex as WebException
The WebException has the full response messages as well and then the code I posted will work in the catch handling code.
Dave,
The webException returned the following:
System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse() at
Constant_Contact.AddNewContactList(String sListName)
It indicated line 337 which is:
Using response AsHttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
Please try the following code sample, this should correctly give you the full error message being returned by us:
catch ex as WebException
Using ex.response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(response.GetResponseStream())
String st = reader.ReadToEnd()
HttpContext.Current.Response.Write(st)
end try
Dave,
I have been able to gleen this from IE, does it help you help us?
Invalid at the top level of the document.
Line: 1 Character: 1
Error 400: The request contains errors in the common Atom sections, which lie outside <entry> or <Content>, such as <feed>, <id>, <author> or <updated>.
The XML created from that code works for me in my test applications, the error we're receiving says that there is a problem with the content. I didn't believe that this would cause an issue, but it could be the encoding method you are using. Both my sample applicatoins and our .NET sample code uses the following encoding option:
Encoding.ASCII.GetBytes()
This would encode it correctly in ASCII encoding, which is the only character encoding set we currently support. If any of the characters you encoded to UTF8 ended up being multi-byte characters, it would correctly cause a 400 error to be returned.
Dave,
Can you pass me the C# code from your sample app that works? At worst I could try and convert it and tesat it hear.
Much appreciated.
Dave,
I changed to the ASCII encoding and I get the exact same error as I previously posted from IE.
This may be a silly question,but are we using the correct URI for this type of request?
https://api.constantcontact.com/ws/customers/" & ctu & "/activities"
Dave,
Another post from me, I was hoping to finish this today, oh well.
I wanted to let you know that I found we had excluded the ContactList open tag in the code, so I added it.
Now I get a 415 media not supported error again.
I appreciate any help.