Problem passing JSON long format parameters to Matlab Production Server

9 views (last 30 days)
I am using MATLAB production server to run functions via REST API. I have a function with 5 input parameters. When I pass parameters using JSON small format (e.g. 5 double) using POST/synchronous method then function returns my result fine. This is my Python code:
conn = http.client.HTTPConnection("serverURL:9910")
headers = { "Content-Type": "application/json"}
body = json.dumps({ "nargout": 1, "rhs" : ['run', inputs] })
conn.request("POST", "/optimFctExample/model", body, headers)
response = conn.getresponse()
If
inputs = [1,2,3,4,5]
then it accepts the rhs just fine. However, I would like to pass more complex parameters than double, and I understand I need to use JSON large format for this.
I tested using
input= [{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [1]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [2]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [3]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [4]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [5]
}]
but then the server returns a 400 error of type:
Error while parsing request:
[err] [worker:31] Expected a data inside special float objects to be a string
What am I doing wrong? is it the way I encaplustae the JSON long format? If I try to pass the original long format lhs (I got from GET) to the rhs, then it works, so the problem is not with the inputs call itself, but rather with the rhs format here. Thanks for your help.

Answers (3)

Kojiro Saito
Kojiro Saito on 18 Jan 2018
Edited: Kojiro Saito on 18 Jan 2018
Instead of mixing small and large JSON formats, please put 'run' parameter to large JSON. The following will work.
input= [{
"mwtype": "char",
"mwsize": [1,3],
"mwdata": ["run"]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [1]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [2]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [3]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [4]
},{
"mwtype": "double",
"mwsize": [1,1],
"mwdata": [5]
}]
body = json.dumps({ "nargout": 1, "rhs" : inputs })

Zebulon
Zebulon on 19 Jan 2018
Edited: Zebulon on 19 Jan 2018
Thanks for the advice, but I get this error:
Too many input arguments.
I should have precised the structure of the model .m fct. It takes two arguments:
function [output] = model(command, inputs)
command is a char type ('run' or 'info') and inputs is an array of MATLAB data types (double or char). From model I dispatch the inputs to 'run' or 'info' functions depending of the command word. Hence the need for two rhs arguemnts there. I suppose I need to encapsulate inputs in a mwdata of type struct or cell then?

Zebulon
Zebulon on 19 Jan 2018
Edited: Zebulon on 19 Jan 2018
I answer to myself: this is working:
command = { "mwtype": "char",
"mwsize": [1,3],
"mwdata": ["run"]
}
inputs = { 'mwdata': [ -1,
8,
"tut",
5,
{ 'mwdata': [ -3, 2],
'mwsize': [1, 2],
'mwtype': 'cell' }
],
'mwsize': [1, 5],
'mwtype': 'cell'}
body = json.dumps({ "nargout": 1, "rhs" : [command, inputs] })
Note that the inputs have to be encapsulated in a cell array or a structure, and then from the .m file access the data using MATLAB cell data acess, e.g. inputs{1} , inputs{2}, inputs{5}{1} etc.
Alternatively, you may use a structure:
inputs = { 'mwdata': {'value': [ {'mwdata': [-3], 'mwsize': [1, 1], 'mwtype': 'double'},
{'mwdata': [6], 'mwsize': [1, 1], 'mwtype': 'double'},
{ 'mwdata': ['tut'],
'mwsize': [1, 3],
'mwtype': 'char'},
{'mwdata': [5], 'mwsize': [1, 1], 'mwtype': 'double'},
{ 'mwdata': [ { 'mwdata': [-3],
'mwsize': [1, 1],
'mwtype': 'double'},
{ 'mwdata': [2],
'mwsize': [1, 1],
'mwtype': 'double'}],
'mwsize': [1, 2],
'mwtype': 'cell'}]},
'mwsize': [1, 5],
'mwtype': 'struct'}
Then you may access data using inputs(1).value and inputs(5).value{1} for instance.
That would be great if the MATLAB Prod server documentation had better examples.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!