Create new contact

Highlighted
New Member

Create new contact

Hi,


I try to create a new contact but I get an error (please see the attachment) 






This is my code:



Public Function POST_Contact_FullInfo(ByVal XMLData As String) As String

 Dim sPassword As String = My.Settings.API_password

        Dim sUsername As String = My.Settings.API_username

        Dim sAPIKey As String = My.Settings.API_Key

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

      

        'setup httpWebReqeust to send the data



        Dim theLoginCredentials As CredentialCache = New CredentialCache()

      

        theLoginCredentials.Add(New Uri(sUri), "Basic", New NetworkCredential((sAPIKey & "%" & sUsername), sPassword))



        Dim address As New Uri(sUri)





        Dim theRequest As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)

        theRequest.Credentials = theLoginCredentials

        theRequest.Method = WebRequestMethods.Http.Post '"POST" '

        theRequest.ContentType = "application/atom+xml"

        theRequest.PreAuthenticate = True

''' I used the same credentials for getting data and they are ok.




        Dim byteArray As Byte() = Encoding.ASCII.GetBytes(XMLData)



        Dim XMLResponse As String = "" '= "Bytes to send: " + byteArray.Length

        Try

            theRequest.ContentLength = byteArray.Length





            '// Create a stream for the POST Request

            Dim streamRequest As Stream = theRequest.GetRequestStream()

            streamRequest.Write(byteArray, 0, byteArray.Length)

            'streamRequest.Close()

            Dim reqResponse As HttpWebResponse

            reqResponse = TryCast(theRequest.GetResponse(), HttpWebResponse)

           

            Dim reader As New StreamReader(reqResponse.GetResponseStream())



            XMLResponse = reader.ReadToEnd()



            '// Close Reader

            reader.Close()



            ' End Using



            '// Close the response to free up the system resources

            'Response.Close()

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try


Please help me with this.


Thanks!


Halford13


 


 

1 REPLY 1
Highlighted
Moderator

It looks like the problems

It looks like the problems all involve bad XML.  There were three errors in the 400 request with the ContactList node, one with the Contact node and you had redundant nodes of StateName and StateCode.  Here is the corrected XML for the 400 request.  The 500 request has similar problems that you can correct as well.


 


 





  1. <entry xmlns="http://www.w3.org/2005/Atom">



  2. <title type="text"></title>



  3. <updated>2008-07-23T14:21:06.407Z</updated>



  4. <author></author>



  5. <id>data:,none</id>



  6. <summary type="text">Contact</summary>



  7. <content type="application/vnd.ctct+xml">



  8. <Contact xmlns="http://ws.constantcontact.com/ns/1.0/">



  9. <EmailAddress>aaaa@bbb.com</EmailAddress>



  10. <FirstName>aaa</FirstName>



  11. <MiddleName></MiddleName>



  12. <LastName>bbb</LastName>



  13. <HomePhone></HomePhone>



  14. <Addr1></Addr1>



  15. <Addr2></Addr2>



  16. <Addr3></Addr3>



  17. <City></City>



  18. <StateName></StateName>



  19. <PostalCode></PostalCode>



  20. <SubPostalCode></SubPostalCode>



  21. <CountryCode></CountryCode>



  22. <CompanyName></CompanyName>



  23. <JobTitle></JobTitle>



  24. <WorkPhone></WorkPhone>



  25. <EmailType>HTML</EmailType>



  26. <OptInSource>ACTION_BY_CONTACT</OptInSource>



  27. <Note></Note>



  28. <CustomField1></CustomField1>



  29. <CustomField2></CustomField2>



  30. <CustomField3></CustomField3>



  31. <CustomField4></CustomField4>



  32. <CustomField5></CustomField5>



  33. <CustomField6></CustomField6>



  34. <CustomField7></CustomField7>



  35. <CustomField8></CustomField8>



  36. <CustomField9></CustomField9>



  37. <CustomField10></CustomField10>



  38. <CustomField11></CustomField11>



  39. <CustomField12></CustomField12>



  40. <CustomField13></CustomField13>



  41. <CustomField14></CustomField14>



  42. <CustomField15></CustomField15>



  43. <ContactLists>



  44. <ContactList id="http://api.constantcontact.com/ws/customers/halford14/lists/5" />



  45. </ContactLists>



  46. </Contact>



  47. </content>



  48. </entry>



Dave Berard
Senior Product Manager, 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