POST Synchronous Request

Make a synchronous request to the server, and wait for a response

Description

Use a POST method to make a synchronous request to the server. In synchronous mode, once a request has been made, the server blocks all further requests until it has completed processing the original request. A response is automatically returned once processing is complete. No other HTTP methods are necessary to retrieve the response from the server.

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 representation, and whether NaN and Inf should be represented as a JSON string or object.

{ "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);

Introduced in R2016a

Was this topic helpful?