404 Bad request error when trying to save email in Constant contact account

SanketS56
Participant

404 Bad request error when trying to save email in Constant contact account

I am using classic ASP code which I have found on this forum but it returns me 400 Error: Bad Request. Check Submitted Data

 

Here is the code snippet:

 

I am trying to pass hardcore first name, last name and so on.......

 

<%
FirstName="San"
LastName="Saxena"
EmailAddress="REMOVED FOR SECURITY PURPOSES"
UN = "REMOVED FOR SECURITY PURPOSES"
PW = "REMOVED FOR SECURITY PURPOSES"
OptInSource="ACTION_BY_CUSTOMER"
API_Key = "REMOVED FOR SECURITY PURPOSES"
List_ID="3" '//Your Particular List ID,,1,2,3 & etc. (Default is 1, first user created would be 2, and so on)

'response.write("Email:"&EmailAddress)
response.write(PostDataToURL(List_ID,FirstName,LastName,EmailAddress, "POST", UN,PW,API_Key))

Function PostDataToURL(List_ID,FirstName,LastName,EmailAddress,strMethod,UN,PW,API_Key)

Dim lngTimeout

Dim strUserAgentString

Dim intSslErrorIgnoreFlags

Dim blnEnableRedirects

Dim blnEnableHttpsToHttpRedirects

Dim strHostOverride

Dim strLogin

Dim strPassword

Dim strResponseText

Dim objWinHttp

Dim entry

Dim Base_URL

Dim UpdateTimeStamp

Base_URL="https://api.constantcontact.com/ws/customers/"&UN&"/"

lngTimeout = 59000

strUserAgentString = "http_requester/0.1"

intSslErrorIgnoreFlags = 0 ' 13056: ignore all err, 0: accept no err

blnEnableRedirects = True

blnEnableHttpsToHttpRedirects = True

strHostOverride = ""

strLogin = API_Key & "%" & UN

strPassword = PW

UpdateTimeStamp = date()

strPostData= "<entry xmlns=""http://www.w3.org/2005/Atom"">"&vbcrlf&_

"<Content-type=""application/vnd.ctct+xml"">"&vbcrlf&_
"<Contact xmlns=""http://ws.constantcontact.com/ns/1.0/"">"&vbcrlf&_
"<EmailAddress>"&EmailAddress&"</EmailAddress>"&vbcrlf&_
"<FirstName>"&FirstName&"</FirstName>"&vbcrlf&_
"<LastName>"&LastName&"</LastName>"&vbcrlf&_
"<OptInSource>"&OptInSource&"</OptInSource>"&vbcrlf&_
"<ContactLists>"&vbcrlf&_
"<ContactList id="""&Base_URL&"lists/"&List_ID&""" />"&vbcrlf&_
"</ContactLists>"&vbcrlf&_
"</Contact>"&vbcrlf&_
"</content>"&vbcrlf&_
"</entry>"' Create your XML to post here or pass it in as part of the method

Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")

objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout

objWinHttp.Open strMethod,Base_URL&"contacts"

If strMethod = "POST" Then
'response.write("POST")
objWinHttp.setRequestHeader "Content-type", "application/atom+xml"
'objWinHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
End If

If strHostOverride <> "" Then

objWinHttp.SetRequestHeader "Host", strHostOverride

End If

objWinHttp.Option(0) = strUserAgentString

objWinHttp.Option(4) = intSslErrorIgnoreFlags

objWinHttp.Option(6) = blnEnableRedirects

objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects

If (strLogin <> "") And (strPassword <> "") Then

objWinHttp.SetCredentials strLogin, strPassword, 0

End If

On Error Resume Next

objWinHttp.Send(strPostData)

If Err.Number = 0 Then

If objWinHttp.Status = "200" Then

PostDataToURL = objWinHttp.ResponseText

Else
'Response.Write(objWinHttp.Status)
select case objWinHttp.Status

case "400"

PostDataToURL="0"&vbtab&":Bad Request. Check Submitted Data"

case else

PostDataToURL = "0"&vbtab&":" & objWinHttp.Status & " " &objWinHttp.StatusText

end select

End If

Else

PostDataToURL = "Error " & Err.Number & " " & Err.Source & " " & _

Err.Description

End If

On Error GoTo 0

Set objWinHttp = Nothing

End Function

%>

1 REPLY 1
DaveBerard
Moderator

Wanted to post a quick update to this.  Your VBScript code doesn't look incorrect, the 400 error is likely due to the XML you're sending.  Is there any chance you can post the actual raw XML that you're sending over?  That is usually more helpful to figure out the problem than the raw code.

 

Also, I updated your post to remove your API key, username and password for security purposes.  I highly recommend changing your login information for Constant Contact.  These are public forums and by posting that information here, it could be viewed by anyone and crawled by search engines.  This information could potentially be cached and stored for people to look up on many websites.  As a general best practice, you should never post a username, password or other identifying information on any forums, public or private, to avoid having your information stolen.

 

P.S. - the original response to this post was removed.  The information in it was not applicable for the VBScript question that was posted.

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page