procedure SendRequest(contentToSend: Variant; RequestMethod: enum "Http Request Type"; requestUri: Text; ContentType: Text; HttpTimeout: integer; DictionaryContentHeaders: Codeunit "Dictionary Wrapper"; DictionaryDefaultHeaders: Codeunit "Dictionary Wrapper"): text
var
Client: HttpClient;
Request: HttpRequestMessage;
Response: HttpResponseMessage;
ContentHeaders: HttpHeaders;
Content: HttpContent;
ResponseText: Text;
ErrorBodyContent: Text;
TextContent: Text;
InStreamContent: InStream;
i: Integer;
KeyVariant: Variant;
ValueVariant: Variant;
HasContent: Boolean;
begin
case true of
contentToSend.IsText():
begin
TextContent := contentToSend;
if TextContent <> '' then begin
Content.WriteFrom(TextContent);
HasContent := true;
end;
end;
contentToSend.IsInStream():
begin
InStreamContent := contentToSend;
Content.WriteFrom(InStreamContent);
HasContent := true;
end;
else
Error(UnsupportedContentToSendErr);
end;
if HasContent then
Request.Content := Content;
if ContentType <> '' then begin
ContentHeaders.Clear();
Request.Content.GetHeaders(ContentHeaders);
if ContentHeaders.Contains(ContentTypeKeyLbl) then
ContentHeaders.Remove(ContentTypeKeyLbl);
ContentHeaders.Add(ContentTypeKeyLbl, ContentType);
end;
for i := 0 to DictionaryContentHeaders.Count() do
if DictionaryContentHeaders.TryGetKeyValue(i, KeyVariant, ValueVariant) then
ContentHeaders.Add(Format(KeyVariant), Format(ValueVariant));
Request.SetRequestUri(requestUri);
Request.Method := Format(RequestMethod);
for i := 0 to DictionaryDefaultHeaders.Count() do
if DictionaryDefaultHeaders.TryGetKeyValue(i, KeyVariant, ValueVariant) then
Client.DefaultRequestHeaders.Add(Format(KeyVariant), Format(ValueVariant));
if HttpTimeout <> 0 then
Client.Timeout(HttpTimeout);
Client.Send(Request, Response);
Response.Content().ReadAs(ResponseText);
if not Response.IsSuccessStatusCode() then begin
Response.Content().ReadAs(ErrorBodyContent);
Error(RequestErr, Response.HttpStatusCode(), ErrorBodyContent);
end;
exit(ResponseText);
end;
var
RequestErr: Label 'Request failed with HTTP Code:: %1 Request Body:: %2', Comment = '%1 = HttpCode, %2 = RequestBody';
UnsupportedContentToSendErr: Label 'Unsuportted content to send.';
ContentTypeKeyLbl: Label 'Content-Type', Locked = true;
local procedure SimpleGET()
var
Result: Text;
begin
Result := SendRequest(Enum::"Http Request Type"::GET, 'https://www.google.com/');
end;
procedure SendRequest(RequestMethod: enum "Http Request Type"; requestUri: Text): text
var
DictionaryDefaultHeaders: Codeunit "Dictionary Wrapper";
DictionaryContentHeaders: Codeunit "Dictionary Wrapper";
ContentType: Text;
begin
exit(SendRequest('', RequestMethod, requestUri, ContentType, 0, DictionaryContentHeaders, DictionaryDefaultHeaders));
end;
CreateBaseXMLDoc('soap12', 'http://www.w3.org/2003/05/soap-envelope')
[local-name()="NODE_NAME"]
/*[local-name()="Envelope"]/*[local-name()="Body"]
Result := XMLDOMManagement.RemoveNamespaces(Result);
//XPath without namespaces
NewTemperature := GetValueFromXML(Result, '/Envelope/Body/FahrenheitToCelsiusResponse/FahrenheitToCelsiusResult');
var
XMLDOMManagement: Codeunit "XML DOM Management";
BaseXMLDoc.SelectSingleNode(BodyXPathLbl, BodyNode);
BodyNode.AsXmlElement().Add(XMLElem);
BaseXMLDoc.WriteTo(ContentToSend);
Result := SendTemperatureConversionAPIRequest(ContentToSend, ContentType, DictionaryDefaultHeaders);
/*[local-name()="Envelope"]/*[local-name()="Body"]/*[local-name()="FahrenheitToCelsiusResponse"]/*[local-name()="FahrenheitToCelsiusResult"]
NewTemperature := GetValueFromXML(Result, ResultXPath);
procedure GetValueFromXML(Content: Text; pNodePath: Text): Text
var
XMLRootNode: XmlNode;
XMLChildNode: XmlNode;
XMLElem: XmlElement;
begin
GetRootNode(ConvertTextToXmlDocument(Content), XMLRootNode);
XMLRootNode.SelectSingleNode(pNodePath, XMLChildNode);
XMLElem := XMLChildNode.AsXmlElement();
exit(XMLElem.InnerText());
end;
procedure Convert(ConvertType: Enum "SDA Temperature Convert Type"; SoapVersion: Enum "SDA SOAP Version"; Temperature: Integer) NewTemperature: Text
var
DictionaryDefaultHeaders: Codeunit "Dictionary Wrapper";
BaseXMLDoc: XmlDocument;
XMLElem: XmlElement;
BodyNode: XmlNode;
ResultXPath: Text;
ContentToSend: Text;
ContentType: Text;
Result: Text;
begin
//Generate base XML request based on SOAP version
//w3schools temperature converter support SOAP 1.1 and SOAP 1.2
//depending on the SOAP version, we need to choose the correct namespace
//SOAPAction is required for SOAP 1.1
case SoapVersion of
SoapVersion::soap:
begin
BaseXMLDoc := CreateBaseXMLDoc(Format(SoapVersion), SOAP11NamespaceURILbl);
ContentType := SOAP11XMLContentTypeLbl;
DictionaryDefaultHeaders.Set('SOAPAction', '');
end;
SoapVersion::soap12:
begin
BaseXMLDoc := CreateBaseXMLDoc(Format(SoapVersion), SOAP12NamespaceUriLbl);
ContentType := SOAP12XMLContentTypeLbl;
end;
end;
//Generate body of request XML document in XMLElem variable
//AddChildElementWithTxtValue() used for append child node to parent (first param)
//Fill ResultXPath variable based on Convert Type
//Set related SOAPAction for SOAP 1.1
case ConvertType of
ConvertType::"To Celsius":
begin
XMLElem := XmlElement.Create('FahrenheitToCelsius', W3BaseNamespaceUriLbl);
AddChildElementWithTxtValue(XMLElem, 'Fahrenheit', W3BaseNamespaceUriLbl, Format(Temperature));
ResultXPath := BodyXPathLbl + LocalXPathSeparatorLbl + StrSubstNo(LocalXPathSignatureLbl, 'FahrenheitToCelsiusResponse') +
LocalXPathSeparatorLbl + StrSubstNo(LocalXPathSignatureLbl, 'FahrenheitToCelsiusResult');
if DictionaryDefaultHeaders.ContainsKey('SOAPAction') then
DictionaryDefaultHeaders.Set('SOAPAction', W3BaseNamespaceUriLbl + 'FahrenheitToCelsius');
end;
ConvertType::"To Farenheit":
begin
XMLElem := XmlElement.Create('CelsiusToFahrenheit', W3BaseNamespaceUriLbl);
AddChildElementWithTxtValue(XMLElem, 'Celsius', W3BaseNamespaceUriLbl, Format(Temperature));
ResultXPath := BodyXPathLbl + LocalXPathSeparatorLbl + StrSubstNo(LocalXPathSignatureLbl, 'CelsiusToFahrenheitResponse') +
LocalXPathSeparatorLbl + StrSubstNo(LocalXPathSignatureLbl, 'CelsiusToFahrenheitResult');
if DictionaryDefaultHeaders.ContainsKey('SOAPAction') then
DictionaryDefaultHeaders.Set('SOAPAction', W3BaseNamespaceUriLbl + 'CelsiusToFahrenheit');
end;
end;
//Append body of request XMLElem to base request XML document
BaseXMLDoc.SelectSingleNode(BodyXPathLbl, BodyNode);
BodyNode.AsXmlElement().Add(XMLElem);
BaseXMLDoc.WriteTo(ContentToSend);
//Send request XML Document and write result to text variable
Result := SendTemperatureConversionAPIRequest(ContentToSend, ContentType, DictionaryDefaultHeaders);
//Read result XML to find result temperature from ResultXPath
NewTemperature := GetValueFromXML(Result, ResultXPath);
//Handle error in case of success response status code (200)
if NewTemperature = ErrorLbl then
Error(ErrorLbl);
end;