Main Content

POST Synchronous Request

Make synchronous request to server, and wait for response

Description

Use a POST method to make a synchronous request to the server. In synchronous mode, after the server receives the request, the worker process on the server blocks all further requests until it has completed processing the original request. The worker automatically returns a response to the client after processing is complete. No other HTTP methods are necessary to retrieve the response from the server.

The server can simultaneously execute as many synchronous requests as the number of available workers.

The following sections use JSON as the data serialization format. For an example that shows how to use protobuf as the data serialization format with the Java® client API, see Synchronous RESTful Requests Using Protocol Buffers in the Java Client, and with the .NET client API, see Synchronous RESTful Requests Using Protocol Buffers in .NET Client.

Request

HTTP Method

POST

URI

http://host:port/deployedArchiveName/matlabFunctionName

Query Parameters

None.

Content-Type

  • application/json

Body

NameDescriptionValue-Type
nargout

Number of outputs that the client application is requesting from the deployed MATLAB® function. Note that MATLAB functions, depending on their intended purpose, can be coded to return multiple outputs. A subset of these potential outputs can be specified using nargout.

number
rhs

Input arguments to the deployed MATLAB function, specified as an array of comma-separated values.

[arg1,arg2,arg3,...]
outputFormat

Specify whether the MATLAB output in the response should be returned using large or small JSON notation, and whether NaN and Inf should be represented as a JSON string or object. If the mode is not specified or the returned MATLAB data type does not support JSON small notation, the response is represented using JSON large notation.

For more information on the JSON representation of MATLAB data types, see JSON Representation of MATLAB Data Types.

{ "mode" : "small | large", "nanInfFormat" : "string | object" }

Example:

Single Input Argument:

{
 "nargout": 1, 
 "rhs": [5],
 "outputFormat": { "mode" : "small", "nanInfFormat" : "object" }
}
Multiple Input Arguments:
{
 "nargout": 2, 
 "rhs": [3, 4, 5 ...],
 "outputFormat": { "mode" : "large", "nanInfFormat" : "string" }
}

Response

Success

HTTP Status Code

200 OK

Body

NameDescriptionValue-Type
lhs

A JSON array contained in the response from the server. Each element of the JSON array corresponds to an output of the deployed MATLAB function represented using JSON notation. For more information on JSON notation see JSON Representation of MATLAB Data Types.

[output1, output2, ...]

Example:

{
"lhs":[[[17,24,1,8,15],[23,5,7,14,16],[4,6,13,20,22],[10,12,19,21,3],[11,18,25,2,9]]]
}

Error

HTTP Status Code

400 InvalidJSON

404 FunctionNotFound

404 ComponentNotFound

Sample Call

HTTP

Request:

POST /mymagic/mymagic HTTP/1.1
Host: localhost:9910 
Content-Type: application/json

{"rhs":[5],"nargout":1,"outputFormat":{"mode":"small","nanType":"string"}}

Response:

Status Code: 200 OK
{
"lhs":[[[17,24,1,8,15],[23,5,7,14,16],[4,6,13,20,22],[10,12,19,21,3],[11,18,25,2,9]]]
}

JavaScript

var data = JSON.stringify({
    "rhs": [5],
    "nargout": 1,
    "outputFormat": {"mode": "small", "nanType": "string"}
});
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
        console.log(this.responseText);
    }
});
xhr.open("POST", "http://localhost:9910/mymagic/mymagic");
xhr.setRequestHeader("content-type", "application/json");
xhr.send(data);

Version History

Introduced in R2016a