XSLT Function HttpRequest

Sends a GET HTTP request and returns its data back.

NAMESPACE

xmlns:AS="http://schema.advantages.cz/AsapFunctions"

SYNTAX

string HttpRequest(string url)
string HttpRequest(string url, string authenticationType, string username, string password)
string HttpRequest(string url, string method, string content, string contentType, XPathNavigator headers)
string HttpRequest(string url, string method, string content, XPathNavigator headers, string authenticationType, string username, string password)

PARAMETERS

Name Description
url Url requested.
authenticationType Type of authentication. Example ‘Basic’
username Login username
password Login password
method Send method, e.g. GET or POST
content Content to send
headers Array of header parameters

REMARKS

If a file is returned as a result of the request, it is encoded as Base64 string.
In order to parse the XML using XPath, the resulting string has to be converted using ToXml function.

HEADERS

Array of header parameters for request.

<xsl:variable name="headerRequest">
	<header name="Content-Encoding" value="gzip" />
	<header name="Option" value="value" />
</xsl:variable>

EXAMPLE

This example loads data from an official company registry web service, extracts data and saves them to the address book.
Source XML

<ROOT>
	<BusinessPartner LegalRegistrationCode="123456789"/>
</ROOT>

Stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
	xmlns:AS="http://schema.advantages.cz/AsapFunctions"
	xmlns:are="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer_basic/v_1.0.3"
	xmlns:D="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_datatypes/v_1.0.3"
	xmlns:date="http://exslt.org/dates-and-times" 
	exclude-result-prefixes="AS date are D">
	<xsl:template match="ROOT">
		<ROOT>
			<xsl:apply-templates select="BusinessPartner"/>
		</ROOT>
	</xsl:template>
	<xsl:template match="BusinessPartner">
		<xsl:if test="string(@LegalRegistrationCode)">
			<!-- get data from a web service -->
			<xsl:variable name="aresResult" select="AS:HttpRequest(concat('http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=', @LegalRegistrationCode))"/>
			<!-- convert data to XML node so it can be accessed using XPath query -->
			<xsl:variable name="aresResultXml" select="AS:ToXml($aresResult)"/>
			<xsl:variable name="error" select="$aresResultXml/are:Ares_odpovedi/are:Odpoved/D:E/D:ET"/>
			<xsl:variable name="zaklad" select="$aresResultXml/are:Ares_odpovedi/are:Odpoved/D:VBAS"/>
			<xsl:variable name="address" select="$zaklad/D:AA"/>
			
			<!-- terminate transformation on error -->
			<xsl:if test="string($error)">
				<xsl:message terminate="yes"><xsl:value-of select="$error"/></xsl:message>
			</xsl:if>
			
			<xsl:copy>
				<!-- copy all attributes -->
				<xsl:copy-of select="@*"/>
				<!-- replace Tax ID attribute with the value from the web service -->
				<xsl:attribute name="TaxIdentificationCode"><xsl:value-of select="$zaklad/D:DIC"/></xsl:attribute>
				<!-- copy all nodes -->
				<xsl:copy-of select="*[name() != 'Name' and name() != 'BusinessPartnerCommunicationChannel']"/>
				<!-- replace the name of the company with the value from the web service -->
				<Name><xsl:value-of select="$zaklad/D:OF"/></Name>
				
				<xsl:choose>
					<!-- if there is no legal address, create it -->
					<xsl:when test="count(BusinessPartnerCommunicationChannel[
						@refBusinessPartnerCommunicationChannelTypeId = 
						AS:GetConstant('BusinessPartnerCommunicationChannelType_AddressLegal')
						]) = 0">
						<BusinessPartnerCommunicationChannel
							Id="{AS:GenerateId()}"
							refBusinessPartnerCommunicationChannelTypeId="{
								AS:GetConstant('BusinessPartnerCommunicationChannelType_AddressLegal')}"
							>
								<xsl:call-template name="SetAddress">
									<xsl:with-param name="address" select="$address"/>
								</xsl:call-template>
						</BusinessPartnerCommunicationChannel>
					</xsl:when>
					<!-- otherwise replace the current address with the one returned by the web service -->
					<xsl:otherwise>
						<xsl:apply-templates select="BusinessPartnerCommunicationChannel[
						@refBusinessPartnerCommunicationChannelTypeId = 
						AS:GetConstant('BusinessPartnerCommunicationChannelType_AddressLegal')
						]">
							<xsl:with-param name="address" select="$address"/>
						</xsl:apply-templates>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:copy>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="BusinessPartnerCommunicationChannel">
		<xsl:param name="address"/>
		
		<xsl:copy>
			<xsl:copy-of select="@*"/>
			<xsl:call-template name="SetAddress">
				<xsl:with-param name="address" select="$address"/>
			</xsl:call-template>
			<xsl:copy-of select="*"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template name="SetAddress">
		<xsl:param name="address"/>
		<xsl:attribute name="Street1"><xsl:value-of select="$address/D:NU"/></xsl:attribute>
		<xsl:attribute name="HouseNumber"><xsl:value-of select="$address/D:CD"/></xsl:attribute>
		<xsl:attribute name="PostalCode"><xsl:value-of select="$address/D:PSC"/></xsl:attribute>
		<xsl:attribute name="City"><xsl:value-of select="$address/D:N"/></xsl:attribute>
	</xsl:template>
</xsl:stylesheet>

EXAMPLE 2

Using httpRequest with authentication.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
	xmlns:AS="http://schema.advantages.cz/AsapFunctions"
	xmlns:date="http://exslt.org/dates-and-times" 
	xmlns:str="http://exslt.org/strings"
    exclude-result-prefixes="AS date str">
	<xsl:param name="text"/>
	<xsl:param name="phoneNumber"/>
	
	<xsl:template match="/">
		
		<xsl:variable name="rawGateway" select="AS:GetConstant('GTS_GatewayURL')"/>
		<xsl:variable name="username" select="AS:GetConstant('GTS_Username')" />
		<xsl:variable name="password" select="AS:GetConstant('GTS_Password')" />
		<xsl:variable name="authType" select="'Basic'" />
				
		<xsl:variable name="sms" select="AS:HttpRequest(concat(
			$rawGateway,
			'?MT_Destination=',	translate($phoneNumber, ' ', ''),
			'&amp;MT_Type=SMS', '&amp;MT_SubType=Text',
			'&amp;MT_Data=',$text), $authType, $username, $password )"/>
		
		<!-- split return code by ;-->
		<xsl:variable name="result" select="str:split($sms, ';')"/>
		
		<ROOT>
		<xsl:choose>
			<xsl:when test="$result[1]='REJECT'">
				<xsl:message terminate="yes"><xsl:value-of select="concat('REJECT:Selhalo odeslání SMS na číslo ', $phoneNumber, '. Chyba: ', $result[2])"/></xsl:message>
			</xsl:when>
			<xsl:when test="$result[1]='ERROR'">
				<xsl:message terminate="yes"><xsl:value-of select="concat('ERROR:Selhalo odeslání SMS na číslo ', $phoneNumber, '. Chyba: ', $result[2])"/></xsl:message>
			</xsl:when>
			<xsl:when test="$result[1]='THROTTLING-ACTIVE'">
				<xsl:message terminate="yes"><xsl:value-of select="concat('Dosažen maximální počet odeslání sms, vyčkejte ', $result[2], ' s.')"/></xsl:message>
			</xsl:when>
		</xsl:choose>
		</ROOT>
	</xsl:template>
	
</xsl:stylesheet>