Can't get API to work, Input string was not in a correct format

Hi All,

Please Help. I have taken the classic Asp sample and am trying to get the api working to add a subscriber.

However when I run the code to call the API I get a message telling me the following;

<faultstring>System.Web.Services.Protocols.SoapException: Server was unable to read request. ---> System.InvalidOperationException: There is an error in XML document (1, 350). ---> System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Xml.XmlConvert.ToInt32(String s) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read22_SubscriberAdd() at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer16.Deserialize(XmlSerializationReader reader) at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) --- End of inner exception stack trace --- at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle) at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters() --- End of inner exception stack trace --- at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</faultstring>


I also get a similar message complaing about the same issue if  I go directly to http://app.campaignmonitor.com/api/api.asmx and run any method through the browser.

I think it is complaining about my ListID which is in the format "1234567890abcdefg01234567890abc" If I change this ListID to be a number (170 for example) then the method returns, but with an Invalid API Key.

However I can't find a simple numeric ID for the list. The only one I can find is the long alphanumeric string.

Can anyone help?

Thanks
Clive.

P.s Included is a sample of my code


dim apikey, listid, name, email, newsletter, postdata, response

apikey = "****************************"
listid = "'2ec63fb373f4baf53a0d89d0c392b519'"

If NomineeLineManager <> "" Then

    postdata = "<?xml version=""1.0"" encoding=""utf-8""?>"
    postdata = postdata & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
    postdata = postdata & "<soap:Body>"
    postdata = postdata & "    <Subscriber.Add xmlns=""http://app.campaignmonitor.com/api/"">"
    postdata = postdata & "        <ApiKey>" & apikey & "</ApiKey>"
    postdata = postdata & "        <ListID>" & listid & "</ListID>"
    postdata = postdata & "        <Email>" & NomineeLineManager & "</Email>"
    postdata = postdata & "        <Name>Tester</Name>"
    postdata = postdata & "    </Subscriber.Add>"
    postdata = postdata & "</soap:Body>"
    postdata = postdata & "</soap:Envelope>"
   

    'Response.Write(postdata)
    Dim xmlhttp
    'set xmlhttp=Server.CreateObject("Microsoft.XMLHTTP")
    'Set xmlhttp=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
    set xmlhttp=Server.CreateObject("MSXML2.ServerXMLHTTP")
   
    xmlhttp.open "POST","http://app.campaignmonitor.com/api/api.asmx" ,false

    xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    xmlhttp.setRequestHeader "SOAPAction", """http://app.campaignmonitor.com/api/Subscriber.Add"""

    xmlhttp.send postdata
   
    response = xmlhttp.responseText
   
    Response.Write response

End If

snowman18, 8 years ago

That list ID is nothing like my ID. I wonder if it correct?

Ken Ken, 8 years ago

Hi Snowman,

Firstly, you have to be careful with your API Key. It's your secret key and you shouldn't let other people get a hold of it. I've hidden here for you.

Could you tell me which application you use, Campaign Monitor or Mailbuild? The format of the list ids for Campaign Monitor are a number and for Mailbuild they are a string. If you are using Mailbuild, replace your open string with

xmlhttp.open "POST","http://<yourdomain>.createsend.com/api/api.asmx" ,false

where <yourdomain> is your special Mailbuild domain.

I hope that helps.


http://www.campaignmonitor.com
clive0s, 8 years ago

Hi Ken, Thanks for that.

I now get a completely different error returning from the API

<faultcode>soap:Client</faultcode>
  <faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://app.campaignmonitor.com/api/Subscriber.Add. at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)</faultstring>

Any sugestions?

Thanks
Clive

clive0s, 8 years ago

Hi,
I've also changed the soap action string to be our specific domain, but I still get a similar error

<faultcode>soap:Client</faultcode>
  <faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://ourdomian.createsend.com/api/Subscriber.Add. at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)</faultstring>

Finally, I've changed the other likely candidate, but still no joy

postdata = postdata & "    <Subscriber.Add xmlns=""http://ourdomain.createsend.com/api/"">"

Pls Help
Clive

Ben Ben, 8 years ago

Hi Clive,

I bet the problem has something to do with mixing the Campaign Monitor and MailBuild API's.

Would you be able to post your full snippet with the changes?

clive0s, 8 years ago

Hi All,

I've tried various things, but still no joy

I've can confirm that I am trying to use the mail builder Api. The strange thing is that the code sample above works when trying to access the campaign monitor Api with random api key and list id, I simply get the error messages telling me that the keys is not recognized, but at least I get a sensible reponse from the api.

I've also tried alternative code to try and call the webservice from my classic ASP form. see below;

Dim webServiceUrl, httpReq, node, myXmlDoc     
    webServiceUrl = "http://<ourdomain>.createsend.com/api/api.asmx/Subscriber.Add?ApiKey=" & apikey & "&ListID=" & listid & "&Email= " &  NomineeLineManager & " &Name=clive"
   
    Set httpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
   
    httpReq.Open "GET", webServiceUrl, False
    httpReq.Send
    response = httpReq.responseText
    Response.Write response

This basically throws up the login page - I get the same result if I simply paste into a browser the path
http://<ourdomain>.createsend.com/api/api.asmx/Subscriber.Add?ApiKey=222222222222222222222222222222&ListID=222222222222222222222222222222222&Email=test@test.com&Name=clive

However if I paste in the corresponding path for campaign monitor
http://app.campaignmonitor.com/api/api.asmx/Subscriber.Add?ApiKey=22&ListID=222&Email=test@test.com&Name=clive
I get a nice response from the webservice telling me the following
<Code>100</Code>
  <Message>Invalid API Key</Message>

Can someone please help me with this. Also if anyone from Campaign Monitor is looking at this please can you help because we are on a tight deadline with the client. We are also in the UK so there is a 10 hr time difference, which means we only get one message in each direction each day.

Pls help
Clive

snowman18, 8 years ago

Are you sure you are using the Campaign Monitor API key and not the mail build API key?

clive0s, 8 years ago

Hi all,

Thanks for attempting to help. I think I may have been getting confused earlier and mixing up the campaignMonitor and Mail Builder API.

I can confirm that it is the Mail Builder Api we need to be using. I can also confirm that we are using the API Key from mail builder and the ListID is a mail builder list.

Howeber we are still getting the following error

<faultcode>soap:Client</faultcode>
  <faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://<yourdomian>.createsend.com/api/Subscriber.Add. at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)</faultstring>
  <detail />


The Code we are using is as follows;

apikey = "1234567890abcdefabcd1234567890ab" 'made up key for purpose of posting to forum
listid = "0987654321bbbbbbbbbb0987654321aa" 'made up key for purpose of posting to forum

    postdata = "<?xml version=""1.0"" encoding=""utf-8""?>"
    postdata = postdata & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
    postdata = postdata & "<soap:Body>"
    postdata = postdata & "    <Subscriber.Add xmlns=""http://<yourdomain>.createsend.com/api/"">"
    postdata = postdata & "        <ApiKey>" & apikey & "</ApiKey>"
    postdata = postdata & "        <ListID>" & listid & "</ListID>"
    postdata = postdata & "        <Email>test@test.com</Email>"
    postdata = postdata & "        <Name></Name>"
    postdata = postdata & "    </Subscriber.Add>"
    postdata = postdata & "</soap:Body>"
    postdata = postdata & "</soap:Envelope>"
   
    Dim xmlhttp
    'set xmlhttp=Server.CreateObject("Microsoft.XMLHTTP")
    'Set xmlhttp=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
    set xmlhttp=Server.CreateObject("MSXML2.ServerXMLHTTP")
   
   xmlhttp.open "POST","http://<yourdomain>.createsend.com/api/api.asmx" ,false
    xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    xmlhttp.setRequestHeader "SOAPAction", """http://<yourdomain>.createsend.com/api/Subscriber.Add"""
    xmlhttp.send postdata
 
    Response.Write xmlhttp.responseText

Thanks
Clive

Stormy Stormy, 8 years ago

I think I may see the problem...

<Subscriber.Add xmlns=""http://<yourdomain>.createsend.com/api/""> - you have <yourdomain> within the <subscriber field. XML doesn't like parsing <> tags within other <> tags. Remove the <> tags around the domain name and hopefully you should see some light, ie:

<Subscriber.Add xmlns=""http://yourdomain.createsend.com/api/"">


No-one expects the Spanish Inquisition!
clive0s, 8 years ago

Sorry Stormy,

but its not that. The <yourdomain> is just a place holder to save me posting our domain on the forums.

My actual code does read <Subscriber.Add xmlns=""http://mydomainnameishere.createsend.com/api/"">

Thanks for the try though
Clive

Ken Ken, 8 years ago

Try this:

apikey = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 'made up key for purpose of posting to forum
listid = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 'made up key for purpose of posting to forum

postdata = "<?xml version=""1.0"" encoding=""utf-8""?>"
postdata = postdata & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
postdata = postdata & "<soap:Body>"
postdata = postdata & " <Subscriber.Add xmlns=""http://api.createsend.com/api/"">"
postdata = postdata & " <ApiKey>" & apikey & "</ApiKey>"
postdata = postdata & " <ListID>" & listid & "</ListID>"
postdata = postdata & " <Email>test@test.com</Email>"
postdata = postdata & " <Name></Name>"
postdata = postdata & " </Subscriber.Add>"
postdata = postdata & "</soap:Body>"
postdata = postdata & "</soap:Envelope>"

'Response.Write(postdata)
Dim xmlhttp
'set xmlhttp=Server.CreateObject("Microsoft.XMLHTTP")
'Set xmlhttp=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
set xmlhttp=Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST","http://<yourspecialdomain>.createsend.com/api/api.asmx" ,false

xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.setRequestHeader "SOAPAction", """http://api.createsend.com/api/Subscriber.Add"""
xmlhttp.send postdata
Response.Write xmlhttp.responseText

I hope that works mate.


http://www.campaignmonitor.com
anthony, 5 years ago

it work for me. the problem accrued in SOAPAction & Post URL.

Thanks so much!

Join 200,000 companies around the world that use Campaign Monitor to run email marketing campaigns that deliver results for their business.

Get started for free
1-888-533-8098