Getting "Bad Request" when Adding Contact Using Visual Basic 6 - HELP

Highlighted
New Member

Getting "Bad Request" when Adding Contact Using Visual Basic 6 - HELP

---------------------------------------------------------
THIS CODE DOES WORK - TO LIST DATA
---------------------------------------------------------
Dim sUsername As String
Dim sPassword As String
Dim sUri As String
Dim sAPIKey As String
Dim sResponse As String
Dim sStatus As String
Dim sEmail As String



sUsername = "xxx"
sPassword = "yyy"
sAPIKey = "zzz"

Select Case iCommand
Case 1: sUri = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists"
Case 2: sUri = "http://api.constantcontact.com/ws/customers/" & sUsername & "/contacts"
Case 3: sUri = "http://api.constantcontact.com/ws/customers/" & sUsername & "/contacts/2"
Case 4:
sEmail = InputBox("enter email", "", "john@smith.com")
sEmail = Replace$(sEmail, "@", "%40")
sUri = "http://api.constantcontact.com/ws/customers/" & sUsername & "/contacts?email=" & sEmail
End Select


Dim xmlhttp As New XMLHTTPRequest
'xmlhttp.open "POST", sUri, False
xmlhttp.open "GET", sUri, False, sAPIKey & "%" & sUsername, sPassword
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send
sResponse = xmlhttp.responseText
sStatus = xmlhttp.statusText
Set xmlhttp = Nothing

Text1.Text = Replace$(sResponse, vbLf, vbCrLf)
lblstatus.Caption = sStatus




---------------------------------------------------------
THIS CODE DOES NOT WORK - GIVES A 'BAD REQUEST' ERROR
- can someone tell me what I'm doing wrong?
---------------------------------------------------------
Dim sUsername As String
Dim sPassword As String
Dim sUri As String
Dim sAPIKey As String
Dim sResponse As String
Dim sStatus As String
Dim sEmail As String
Dim sXMLData As String

sXMLData = ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & " "
'sXMLData = sXMLData & "2008-07-23T14:21:06.407Z"
sXMLData = sXMLData & "" & Format$(Now(), "Y-m-d\TH:i:s") & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & "data:,none"
sXMLData = sXMLData & "Active"
sXMLData = sXMLData & "support@mark-it-systems.com"
sXMLData = sXMLData & "HTML"
sXMLData = sXMLData & "John Smith"
sXMLData = sXMLData & "John"
sXMLData = sXMLData & ""
sXMLData = sXMLData & "Smith"
sXMLData = sXMLData & ""
sXMLData = sXMLData & "Smith and Smith"
sXMLData = sXMLData & ""
sXMLData = sXMLData & "(417)555-1212"
sXMLData = sXMLData & "111 S. Main"
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & "AnyTown"
sXMLData = sXMLData & "MO"
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & "65613"
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & "ACTION_BY_CONTACT"
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""
sXMLData = sXMLData & "
"
sXMLData = sXMLData & "
"
sXMLData = sXMLData & ""
sXMLData = sXMLData & ""




sUsername = "xxx"
sPassword = "yyy"
sAPIKey = "zzz"

sUri = "http://api.constantcontact.com/ws/customers/" & sUsername & "/contacts"
Dim xmlhttp As New XMLHTTPRequest
xmlhttp.open "POST", sUri, False, sAPIKey & "%" & sUsername, sPassword
xmlhttp.setRequestHeader "Content-Type", "application/atom+xml"
xmlhttp.send sXMLData

sResponse = xmlhttp.responseText
Debug.Print sResponse

sStatus = xmlhttp.statusText
Debug.Print sStatus


Set xmlhttp = Nothing

Text1.Text = Replace$(sResponse, vbLf, vbCrLf)
lblstatus.Caption = sStatus
1 REPLY 1
Moderator

I'm not familiar with the

I'm not familiar with the XMLWebRequest class as this is only used in IE AJAX programming, all of our sample scripts use the standard WebRequest or HTTPWebRequest classes, both of which support standard HTTP requests. The 400 error typically means your XML file is incorrect or in an invalid encoding.

A good way to do a sanity check is to dump out your XML code and run it through the REST client. This will give you a very easy to use testing environment. You can download the updated client here. If the XML document goes through there without problems, I would recommend using the HTTPWebRequest class instead of the XMLWebRequest to ensure that the set up is correct and you are posting the correct login credentials and to the correct URI.
Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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