webMathematica >

Web Services

Mathematica can work as a client for web services. The HTTP and XML tools are good for informal web services, while the Web Services Link, allows you to call to use SOAP web services. Here, you can see how to use webMathematica as a server for web services.

Informal Web Services

AJAX Example

This example uses AJAX technology to demonstrate the setting up of an informal web service. It will not discuss all the details of how AJAX works, which is covered in the section on using AJAX with webMathematica.

Mathematica SOAP Client

Mathematica provides a nice interface for calling SOAP web services with the Web Services Link. This is complemented by webMathematica, which provides functionality for publishing SOAP web services. It is quite convenient to use the Mathematica client tools in order to test the server tools.
The first step to using a web service in Mathematica is to install it. This is done with the InstallService function; giving the URL that describes the service, it returns a list of functions that are provided by the service. An example is shown below (note that this service comes from a third party and might stop working at some time in the future).
In[1]:=
Out[1]=
In[3]:=
Out[3]=
In[3]:=
Out[3]=

webMathematica SOAP Services

Echo Example

In[1]:=
Out[1]=
In[3]:=
Out[3]=
This is written as a Mathematica package: the contents are encapsulated with BeginPackage and EndPackage. You can read more about this in the tutorial on setting up packages. However it has a few additional elements: you need to also import the WebServicesServer` and XMLSchema` packages. Also the public functions, which will be published as web services, need to have input patterns that the server accepts, and the return value of the function needs to be given with the ServiceReturn setting. Typical Mathematica functions do not need to specify any type information, because the system is dynamically typed. However, SOAP web services need this information.

Plot Example

In[2]:=
Out[2]=
In[4]:=
Out[5]//Short=
This turns the bytes into a string, and uses ImportString to display the image that was generated.
In[6]:=
Out[6]=
In[7]:=
Out[7]=

Excel Example

Type Specification

Web services data types.

Simple Data

Simple data types.

TestInteger[int_Integer] := MatchQ[ int, _Integer]
In[1]:=
Out[1]=
In[2]:=
Out[2]=

Date and Time Data

Date and time data types.

TestDateTime[dt_SchemaDateTime] := MatchQ[ dt, SchemaDateTime[{y_,m_,d_,h_,m_,s_}]]
In[1]:=
Out[1]=
In[2]:=
Out[2]=

Binary Data

Binary data type.

TestBinaryData[b_SchemaBase64Binary] := 
MatchQ[ b, SchemaBase64Binary[{__Integer}]]
In[1]:=
Out[1]=
In[3]:=
Out[3]=

SchemaExpr

Expression data type.

In[1]:=
Out[1]=
In[1]:=
Out[1]=
In[2]:=
Out[2]=

SchemaMathML

MathML data type.

In[1]:=
Out[1]=
In[2]:=
Out[2]=
In[3]:=
Out[3]=
In[4]:=
Out[4]=

Arrays

Array data type.

TestIntegerArray[list:{___Integer}] := MatchQ[ list, {___Integer}]

TestRealArray[list:{___Real}] := MatchQ[ list, {___Real}]
In[1]:=
Out[1]=
In[2]:=
Out[2]=

Errors and Exceptions

In[1]:=
Out[1]=
In[2]:=
Out[2]=
In[3]:=
Out[3]=
In[2]:=
Out[2]=
In[3]:=
Out[3]=
In[4]:=
Out[4]=
DayOfTheWeek[year_Integer, month_Integer, day_Integer] :=
Module[{dayOfWeek},
If[!DateQ[{year, month, day}],
Throw[ServiceException["Invalid date: " <> ToString[{year, month, day}]]]
];
ToString[ DayOfWeek[{year, month, day}]]
]

Security

In[1]:=
Out[1]=
In[2]:=
Out[2]=
EchoExpression[expr_SchemaExpr] := expr
ServiceReturn[EchoExpression] = _SchemaExpr
In[3]:=
Out[3]=
In[4]:=
Out[4]=