Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Magyar Nemzeti Bank (MNB) Get Currencies
See more REST Misc Examples
The Magyar Nemzeti Bank (MNB) is the central bank of Hungary. In this role, its primary objective is to achieve and maintain price stability. The MNB website is available to visitors in both Hungarian and English. The MNB provides the Arfolyam (trans. Exchange Rate) API, which can be used to retrieve current and historic currency exchange rates. This service uses SOAP calls issued in XML format.This example gets a list of currencies.
Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.StringBuilder,
Chilkat.Rest,
Chilkat.Xml;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
xml: TXml;
sbRequestBody: TStringBuilder;
sbResponseBody: TStringBuilder;
respStatusCode: Integer;
xmlResponse: TXml;
resultEncoded: string;
sbResult: TStringBuilder;
xmlResult: TXml;
begin
success := False;
rest := TRest.Create;
// URL: http://www.mnb.hu/arfolyamok.asmx
bTls := False;
port := 80;
bAutoReconnect := True;
success := rest.Connect('www.mnb.hu',port,bTls,bAutoReconnect);
if (success <> True) then
begin
WriteLn('ConnectFailReason: ' + rest.ConnectFailReason);
WriteLn(rest.LastErrorText);
Exit;
end;
// --------------------------------------------------------------------------------
// Also see Chilkat's Online WSDL Code Generator
// to generate code and SOAP Request and Response XML for each operation in a WSDL.
// --------------------------------------------------------------------------------
xml := TXml.Create;
xml.Tag := 'soapenv:Envelope';
xml.AddAttribute('xmlns:soapenv','http://schemas.xmlsoap.org/soap/envelope/');
xml.AddAttribute('xmlns:web','http://www.mnb.hu/webservices/');
xml.UpdateChildContent('soapenv:Header','');
xml.UpdateChildContent('soapenv:Body|web:GetCurrencies','');
rest.AddHeader('Content-Type','text/xml');
rest.AddHeader('SOAPAction','/webservices/MNBArfolyamServiceSoap/GetCurrencies');
rest.AddHeader('Accept','application/xml');
sbRequestBody := TStringBuilder.Create;
xml.GetXmlSb(sbRequestBody);
sbResponseBody := TStringBuilder.Create;
success := rest.FullRequestSb('POST','/arfolyamok.asmx',sbRequestBody,sbResponseBody);
if (success <> True) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
respStatusCode := rest.ResponseStatusCode;
if (respStatusCode >= 400) then
begin
WriteLn('Response Status Code = ' + respStatusCode);
WriteLn('Response Header:');
WriteLn(rest.ResponseHeader);
WriteLn('Response Body:');
WriteLn(sbResponseBody.GetAsString());
Exit;
end;
WriteLn('response status code = ' + respStatusCode);
xmlResponse := TXml.Create;
xmlResponse.LoadSb(sbResponseBody,True);
WriteLn(xmlResponse.GetXml());
// The XML response contains this:
// <?xml version="1.0" encoding="utf-8" ?>
// <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
// <s:Body>
// <GetCurrenciesResponse xmlns="http://www.mnb.hu/webservices/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
// <GetCurrenciesResult><MNBCurrencies><Currencies><Curr>HUF</Curr><Curr>EUR</Curr><Curr>AUD</Curr> ... </MNBCurrencies></GetCurrenciesResult>
// </GetCurrenciesResponse>
// </s:Body>
// </s:Envelope>
// Get the GetCurrenciesResult
resultEncoded := xmlResponse.GetChildContent('s:Body|GetCurrenciesResponse|GetCurrenciesResult');
WriteLn(resultEncoded);
// Entity decode the result to get XML.
sbResult := TStringBuilder.Create;
sbResult.Append(resultEncoded);
sbResult.EntityDecode();
// Load it into XML.
xmlResult := TXml.Create;
xmlResult.LoadSb(sbResult,True);
WriteLn(xmlResult.GetXml());
// The result is this:
// <?xml version="1.0" encoding="utf-8" ?>
// <MNBCurrencies>
// <Currencies>
// <Curr>HUF</Curr>
// <Curr>EUR</Curr>
// <Curr>AUD</Curr>
// <Curr>BGN</Curr>
// <Curr>BRL</Curr>
// <Curr>CAD</Curr>
// <Curr>CHF</Curr>
// <Curr>CNY</Curr>
// <Curr>CZK</Curr>
// <Curr>DKK</Curr>
// <Curr>GBP</Curr>
// <Curr>HKD</Curr>
// <Curr>HRK</Curr>
// <Curr>IDR</Curr>
// <Curr>ILS</Curr>
// <Curr>INR</Curr>
// <Curr>ISK</Curr>
// <Curr>JPY</Curr>
// <Curr>KRW</Curr>
// <Curr>MXN</Curr>
// <Curr>MYR</Curr>
// <Curr>NOK</Curr>
// <Curr>NZD</Curr>
// <Curr>PHP</Curr>
// <Curr>PLN</Curr>
// <Curr>RON</Curr>
// <Curr>RSD</Curr>
// <Curr>RUB</Curr>
// <Curr>SEK</Curr>
// <Curr>SGD</Curr>
// <Curr>THB</Curr>
// <Curr>TRY</Curr>
// <Curr>UAH</Curr>
// <Curr>USD</Curr>
// <Curr>ZAR</Curr>
// <Curr>ATS</Curr>
// <Curr>AUP</Curr>
// <Curr>BEF</Curr>
// <Curr>BGL</Curr>
// <Curr>CYN</Curr>
// <Curr>CSD</Curr>
// <Curr>CSK</Curr>
// <Curr>DDM</Curr>
// <Curr>DEM</Curr>
// <Curr>EEK</Curr>
// <Curr>EGP</Curr>
// <Curr>ESP</Curr>
// <Curr>FIM</Curr>
// <Curr>FRF</Curr>
// <Curr>GHP</Curr>
// <Curr>GRD</Curr>
// <Curr>IEP</Curr>
// <Curr>ITL</Curr>
// <Curr>KPW</Curr>
// <Curr>KWD</Curr>
// <Curr>LBP</Curr>
// <Curr>LTL</Curr>
// <Curr>LUF</Curr>
// <Curr>LVL</Curr>
// <Curr>MNT</Curr>
// <Curr>NLG</Curr>
// <Curr>OAL</Curr>
// <Curr>OBL</Curr>
// <Curr>OFR</Curr>
// <Curr>ORB</Curr>
// <Curr>PKR</Curr>
// <Curr>PTE</Curr>
// <Curr>ROL</Curr>
// <Curr>SDP</Curr>
// <Curr>SIT</Curr>
// <Curr>SKK</Curr>
// <Curr>SUR</Curr>
// <Curr>VND</Curr>
// <Curr>XEU</Curr>
// <Curr>XTR</Curr>
// <Curr>YUD</Curr>
// </Currencies>
// </MNBCurrencies>
//
rest.Free;
xml.Free;
sbRequestBody.Free;
sbResponseBody.Free;
xmlResponse.Free;
sbResult.Free;
xmlResult.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.