Main Content

matlab.rest_function_service.client.MWHttpClient Class

R2026b

Python client interface for calling MATLAB REST function service

Since R2026b

Description

The Python® class matlab.rest_function_service.client.MWHttpClient creates an object for calling MATLAB® functions in a REST function service. Use this object in your Python client applications to pass inputs to MATLAB functions using native Python data types. Data is passed synchronously using the HTTPS request-response model.

Creation

Before creating a matlab.rest_function_service.client.MWHttpClient object, follow these steps to make the class accessible from your Python application:

  1. Install the Python Client for MATLAB REST Function Service package from the Python Package Index (PyPI). At the operating system command line, run this command.

    python -m pip install matlab-restfcnservice-client
  2. In your Python application, import the matlab package and MWHttpClient class from Python Client for REST Function Service.

    import matlab
    from matlab.rest_function_service.client import MWHttpClient

Then create an object of the matlab.rest_function_service.client.MWHttpClient class using one of the syntaxes shown below.

Description

client = MWHttpClient(base_url=url,rest_personal_access_token=token,certificate=cert) creates an object for calling the MATLAB REST function service at the specified URL. The service authenticates the client using the specified access token and certificate file.

example

client = MWHttpClient(___,timeout_sec=timeout) also specifies how many seconds the Python application waits for a response from the MATLAB REST function service before timing out. Specify the timeout value after all the input arguments from the previous syntax.

example

Input Arguments

expand all

HTTPS request URL used to call the MATLAB REST function service, specified as a Python string scalar. Obtain this URL from the RequestUrl property of the ClientRequestInfo object that MATLAB returns when you start the service. For more details, see Create MATLAB REST Function Services.

Do not include the service name in url. For example, if the RequestUrl property is "https://localhost:9920/feval/v1/myService", then specify url as "https://localhost:9920/feval/v1".

Trailing slashes in url are ignored.

Example: "https://localhost:9920/feval/v1"

Example: "https://localhost:9920/feval/v1/"

Data Types: str

Personal access token required to call the MATLAB REST function service, specified as a Python string scalar. Obtain this token from the RESTPersonalAccessToken property of the ClientRequestInfo object that MATLAB returns when you start the service. For more details, see Create MATLAB REST Function Services.

Example: "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyA="

Data Types: str

Certificate file required to call the MATLAB REST function service, specified as a Python string scalar representing the full path to a PEM file. Obtain this file path from the CertificateLocation property of the ClientRequestInfo object that MATLAB returns when you start the service. For more details, see Create MATLAB REST Function Services.

To prevent Python from interpreting Windows® backslashes as escape characters, format cert as a raw string.

Example: r"C:\Users\username\AppData\Roaming\MathWorks\restfcnconnector\publickey.pem"

Data Types: str

Timeout value, in seconds, specified as a positive Python integer. If the client application does not receive a response from the MATLAB REST function service after this number of seconds, the request times out with an error.

Example: 15

Data Types: int

Methods

expand all

Examples

collapse all

In your Python application file, create a Python REST client for calling a MATLAB REST function service.

import matlab
from matlab.rest_function_service.client import MWHttpClient

url = "https://localhost:9920/matlab/feval/v1"
token = "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyA="
cert = r"C:\Users\user\AppData\Roaming\MathWorks\restfcnconnector\publickey.pem"

client = MWHttpClient(
            base_url=url,
            rest_personal_access_token=token,
            certificate=cert)

For an example of how to call MATLAB functions using this client, see Call MATLAB Functions from Python Using REST.

In your Python application file, create a Python REST client that times out if it does not receive a response from the MATLAB REST function service after one minute.

import matlab
from matlab.rest_function_service.client import MWHttpClient

url = "https://localhost:9920/matlab/feval/v1"
token = "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyA="
cert = r"C:\Users\user\AppData\Roaming\MathWorks\restfcnconnector\publickey.pem"
timeout = 60

client = MWHttpClient(
            base_url=url,
            rest_personal_access_token=token,
            certificate=cert,
            timeout_sec=timeout)

More About

expand all

Version History

Introduced in R2026b