Examples

Below are some examples of PSP pages performing RPC requests. A typical server-side PSP script taking care of a RPC request has the form:

<!DOCTYPE html>
<html xmlns:psp="http://www.prologwebservices.org/psp" psp:prolog="
ensure_loaded('...'),
  .
  .
  .
ensure_loaded('...')
">
</html>

The page loads Prolog databases located on the remote server that are supposed to be relevant to the goal sent via the RPC request. Notice that any output given during the processing of the RPC request on the server-side is suppressed. It is recommended to place the Prolog code in the html tag.

Example 1: Fill a table with RPC data

Let's assume that a remote web service stores a dynamic database about currency rates. The server provides a exchange_rate/3 predicate with mode exchange_rate(+BaseCurrencyCode, +TargetCurrencyCode, -ExchangeRate). A client PSP page receiving the target currency code (i.e. base) and filling a table with eschange rates relative to it can be written as:

<!DOCTYPE html>
<html xmlns:psp="http://www.prologwebservices.org/psp" psp:prolog="
use_module(library(http/http_parameters)),
http_current_request(Request),
http_parameters(Request, [base(Base, [default('USD')])]),
URL = 'http://localhost/psp/exchange_rates_server.psp',
http_findall(Code-Name, URL:currency(Code, Name, _), List)
">
<body>
<p>
<b psp:prolog="format('Exchange Rates (Base Currency: ~w)', [Base])"></b>
<form method="get" action="">
<select required="required" name="base" psp:prolog="
select(Code-Name, List, _),
(Code = Base -> Selected = 'selected' ; Selected = false)
">
<option
psp:selected="Selected \= false, write(Selected)"
psp:value="write(Code)"
psp:prolog="format('~w (~w)', [Name, Code])">
</option>
</select>
<input type="submit" value="Submit">
</form>
<br>
<table psp:prolog="http_call(URL:(currency(Code, Name, _), exchange_rate(Base, Code, Rate)))">
<tr>
<td psp:prolog="write(Code)"></td>
<td psp:prolog="write(Name)"></td>
<td psp:prolog="write(Rate)"></td>
</tr>
</table>
</p>
</body>
</html>
Last update: Mon Nov 02 00:37:21 2015