Skip to content

Call Web Services from GoAnywhere

Looking for an example project to get you started?
  • Support_Steve Offline
  • Support Specialist
  • Posts: 11
  • Joined: Fri May 08, 2009 8:59 am

Call Web Services from GoAnywhere

Post by Support_Steve »

GoAnywhere has the ability to call web services by utilizing our XML and HTTP(S) Tasks. This Project example will query a database table, build a SOAP request, and post it to the web service.

For this example Project I will be using a public web service hosted on http://www.webservicex.net that expects a zip code as input and returns the weather forecast for that area. Below is the SOAP envelope that the web service is expecting.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetWeatherByZipCode xmlns="http://www.webservicex.net">
      <ZipCode>68003</ZipCode>
    </GetWeatherByZipCode>
  </soap12:Body>
</soap12:Envelope>
If you are calling a web service with static data this XML will work fine. In this example I will be creating the XML from the GoAnywhere Project and substituting the zip code with dynamic data from a database. Calling this web service will return the following response XML.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soap:Body>
		<GetWeatherByZipCodeResponse xmlns="http://www.webservicex.net">
			<GetWeatherByZipCodeResult>
				<Latitude>41.05411</Latitude>
				<Longitude>96.39041</Longitude>
				<AllocationFactor>0.002088</AllocationFactor>
				<FipsCode>31</FipsCode>
				<PlaceName>ASHLAND</PlaceName>
				<StateCode>NE</StateCode>
				<Details>
					<WeatherData>
						<Day>Thursday, May 21, 2009</Day>
						<WeatherImage>http://forecast.weather.gov/images/wtf/sct.jpg</WeatherImage>
						<MaxTemperatureF>79</MaxTemperatureF>
						<MinTemperatureF>55</MinTemperatureF>
						<MaxTemperatureC>26</MaxTemperatureC>
						<MinTemperatureC>13</MinTemperatureC>
					</WeatherData>
					<WeatherData>
						<Day>Friday, May 22, 2009</Day>
						<WeatherImage>http://forecast.weather.gov/images/wtf/tsra30.jpg</WeatherImage>
						<MaxTemperatureF>77</MaxTemperatureF>
						<MinTemperatureF>58</MinTemperatureF>
						<MaxTemperatureC>25</MaxTemperatureC>
						<MinTemperatureC>14</MinTemperatureC>
					</WeatherData>
					<WeatherData>
						<Day>Saturday, May 23, 2009</Day>
						<WeatherImage>http://forecast.weather.gov/images/wtf/tsra30.jpg</WeatherImage>
						<MaxTemperatureF>79</MaxTemperatureF>
						<MinTemperatureF>59</MinTemperatureF>
						<MaxTemperatureC>26</MaxTemperatureC>
						<MinTemperatureC>15</MinTemperatureC>
					</WeatherData>
					<WeatherData>
						<Day>Sunday, May 24, 2009</Day>
						<WeatherImage>http://forecast.weather.gov/images/wtf/tsra30.jpg</WeatherImage>
						<MaxTemperatureF>79</MaxTemperatureF>
						<MinTemperatureF>59</MinTemperatureF>
						<MaxTemperatureC>26</MaxTemperatureC>
						<MinTemperatureC>15</MinTemperatureC>
					</WeatherData>
					<WeatherData>
						<Day>Monday, May 25, 2009</Day>
						<WeatherImage>http://forecast.weather.gov/images/wtf/tsra20.jpg</WeatherImage>
						<MaxTemperatureF>78</MaxTemperatureF>
						<MinTemperatureF>58</MinTemperatureF>
						<MaxTemperatureC>26</MaxTemperatureC>
						<MinTemperatureC>14</MinTemperatureC>
					</WeatherData>
					<WeatherData>
						<Day>Tuesday, May 26, 2009</Day>
						<WeatherImage>http://forecast.weather.gov/images/wtf/tsra30.jpg</WeatherImage>
						<MaxTemperatureF>75</MaxTemperatureF>
						<MinTemperatureF>56</MinTemperatureF>
						<MaxTemperatureC>24</MaxTemperatureC>
						<MinTemperatureC>13</MinTemperatureC>
					</WeatherData>
					<WeatherData />
				</Details>
			</GetWeatherByZipCodeResult>
		</GetWeatherByZipCodeResponse>
	</soap:Body>
</soap:Envelope>
Now that we know what the XML request and response should look like lets build the project. First, I'm going to create a workspace to store the XML files in a temporary directory. Then I'm going to perform an SQL query to return the zip code based on a company name. In the example I hard coded the company name to 'Linoma Software'. If you would like you can replace this static text with a project variable, which will allow you to override the company name at runtime.

The next task is to create an XML file that mimics the request format shown above and pass in the zip code to the appropriate element. The project will then use the HTTP Task to post this data to the web service. The response of the post (response XML format shown above) will be written to C:\temp\SOAPResponse.xml. Although not described in this topic I could then use the XML Read Task to grab the data, load it into a database, convert the data to another file format, email the information and more.

Here is the Project XML:

Code: Select all

<project name="SOAP call" mainModule="Main" version="1.0">

	<module name="Main">

		<createWorkspace />

		<sql resourceId="Production 400">
			<query outputVariable="data">
				<statement>select zipcode from gademo.websrvdemo where company = &apos;Linoma Software&apos;</statement>
			</query>
		</sql>

		<xmlWrite outputFile="SOAPRequest.xml">
			<header><?xml version="1.0" encoding="UTF-8" ?></header>
			<element name="soap12:Envelope">
				<element name="soap12:Body">
					<forEach inputRowSetVariable="${data}">
						<element name="GetWeatherByZipCode">
							<attribute name="xmlns" value="http://www.webservicex.net" />
							<element name="ZipCode" value="${data[1]}" />
						</element>
					</forEach>
				</element>
				<attribute name="xmlns:xsi" value="http://www.w3.org/2001/XMLSchema-instance" />
				<attribute name="xmlns:xsd" value="http://www.w3.org/2001/XMLSchema" />
				<attribute name="xmlns:soap12" value="http://www.w3.org/2003/05/soap-envelope" />
			</element>
		</xmlWrite>

		<http host="www.webservicex.com">
			<postRawData uri="/WeatherForecast.asmx " inputFile="SOAPRequest.xml" contentType="application/soap+xml; charset=utf-8" responseBodyDestination="file" responseBodyFile="C:\temp\SOAPResponse.xml" whenResponseBodyFileExists="overwrite" />
		</http>

		<deleteWorkspace />

	</module>

</project>
Note: This project works on all versions of GoAnywhere