VB.NET 400 Bad Request response

Highlighted
Occasional Participant

VB.NET 400 Bad Request response

I'm trying to get the VB.NET 'Add a Contact' example that's posted in a few different topics here to work, and I keep getting a (400) Bad Request response.

Here's my code... any help would be appreciated.

Dim sUsername As String = "myUserName"
Dim sPassword As String = "myPassword"
Dim sUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/activities"
Dim sListUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists/1"
Dim sAPIKey As String = "myAPIKey"

'Setup an HttpWebRequest
Dim address As New Uri(sUri)
Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)
request.Credentials = New NetworkCredential((sAPIKey & "%" & sUsername), sPassword)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"

'Build data string
Dim data As New StringBuilder()
data.Append("activityType=" + HttpUtility.UrlEncode("ADD_CONTACTS", Encoding.UTF8))
data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name" & Chr(10)), Encoding.UTF8))
data.Append(HttpUtility.UrlEncode((txtEmail.Text & ", HTML, " & txtFirstName.Text & ", " & txtLastName.Text), Encoding.UTF8))
data.Append("&lists=" + HttpUtility.UrlEncode(sListUri))

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
7 REPLIES 7
Highlighted
Moderator

RE: VB.NET 400 Bad Request response

A 400 Bad Request error is usually returned when either the XML file is incorrect or the URI you are posting to is incorrect.

It looks like the URI you are setting up:

Dim sUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/activities"

Is correct for the Bulk Activites collection. The rest of the XML data building looks correct. The first thing I would look at would be the list number. List 1 is the General Interest list that is the default in every account. Do you still have that list existing or are you trying to add to an non-existent list?
Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Now it's a (415) Unsupported Media Type response...

Ok, so I changed the URI to "http://api.constantcontact.com/ws/customers/" & sUsername & "/contacts"

You are correct about the list number... so after some trial and error I found an existing list, but now I'm getting a (415) Unsupported Media Type response.
Highlighted
Developer

re: Unsupported Media Type

The POST body for the Contacts resource is very different from the body for the Activities resource. The supported content-types are different as well.

I suggest that you change the URI back to:

"http://api.constantcontact.com/ws/customers/" & sUsername & "/activites"

and use the correct existing list URI, which you indicate you found.

Once you get that working - you can consider changing over to use the Contacts Resource. The Contacts resource should be used for individual contact adds/updates. The Activites resource should be used for bulk upload and update of Contacts.

Thanks,
Tom M
Group Product Manager – Content Editing
Constant Contact
Highlighted
Occasional Participant

Square One

Well, now I'm back to (400) Bad Request.
Highlighted
Moderator

RE: VB.NET 400 Bad Request response

If you are back to a 400 Bad Request, there are very few causes of this error. I opened up my VB test program and added your code as a function. Running it as is, with only my UN/PW/API Key added with some dummy email sign up information, worked straight from this page. I have a list 1 so this did not cause any 400 errors.

Have you changed the list number to reflect a valid list number from your account? Also, what is the URI of the list you are sending the request to? Provided those two URIs are correct, the only error codes you should see are 401, bad UN or PW, or 415, your encoded URI or content type is incorrect.

Also, I see that you used the following variables:

txtEmail
txtFirstName
txtLastName

Have you tried using string literals in place of those variables for testing purposes? Also, what type are those variables?
Dave Berard
Senior Product Manager, Constant Contact
Highlighted
New Member

What is the correct URI to Post Adding a Contact?

What is the correct URI for the POST for adding a contact? The documentation said the collection uri...so I'm using


https://api.constantcontact.com/ws/customers/" + UserName + "/contacts"I'm also getting the 400 Bad Request Error....I did track down that a valid list ID is 52 and made that change.


 


 


 


Any help would be appreciated...btw I'm using c#.net

Highlighted
CTCT Employee

You are posting to the

You are posting to the correct URI and provided that your list ID is valid, you may just have a formatting error in your XML. The cause of the 400 error could be as simple as missing a required node. If you would like to post or email me the xml that is being submitted I would be happy to see if there are any errors.


 


Might I also recommend using the RESTClient to troubleshoot your XML and isolate the issue to either your program code, or the XML.


 


 

Dan H

Support Engineer, Constant Contact

Developer Portal

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

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured