How to convert legacy SoapService call to new UrlFetchApp

I found an example of how to call a web service using scripts on a Google script: https://developers.google.com/apps-script/articles/soap_geoip_example

function determineCountryFromIP(ipAddress) { var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx?wsdl"); var geoService = wsdl.getGeoIPService(); var param = Xml.element("GetGeoIP", [ Xml.attribute("xmlns", "http://www.webservicex.net/"), Xml.element("IPAddress", [ ipAddress ]) ]); var result = geoService.GetGeoIP(param); return result.Envelope.Body.GetGeoIPResponse.GetGeoIPResult.CountryCode.Text; } 

However, it uses a SoapService, which is deprecated. the documentation says that I should use UrlFetchApp Converting xml input is very simple. But can someone tell me how to call a web service using UrlFetchApp?

+6
source share
1 answer

It turns out a lot more work, but after a day of searching the Internet and trying, I got it to work with UrlFetchApp

 function UrlFetchAppDetermineCountryFromIP_(ipAddress) { var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +"<SOAP-ENV:Body>" +"<GetGeoIP xmlns=\"http://www.webservicex.net/\">" +"<IPAddress>"+ ipAddress +"</IPAddress>" +"</GetGeoIP>" +"</SOAP-ENV:Body>" +"</SOAP-ENV:Envelope>" var options = { "method" : "post", "contentType" : "text/xml", "payload" : xml }; var result = UrlFetchApp.fetch("http://www.webservicex.net/geoipservice.asmx?wsdl", options); var xmlResult = XmlService.parse(result).getRootElement(); var soapNamespace = xmlResult.getNamespace("soap"); var getGeoIPResponse = xmlResult.getChild("Body", soapNamespace).getChildren()[0]; var getGeoIPResponseNamespace = getGeoIPResponse.getNamespace(); return getGeoIPResponse .getChild("GetGeoIPResult", getGeoIPResponseNamespace) .getChild("CountryCode", getGeoIPResponseNamespace) .getText(); } 

It would be nice to build the xml payload using XmlService , however I tried this for several hours and could not put 4 xmlns attributes on the Evnelope Element, which causes the web service request to fail

+8
source

Source: https://habr.com/ru/post/950427/


All Articles