How can I POST JSON arguments (Request Payload) using MATLAB's webwrite?

8 ビュー (過去 30 日間)
Fausto Soares
Fausto Soares 2019 年 10 月 22 日
回答済み: Mudabbir Bhatti 2022 年 7 月 18 日
I am using a data acquisition module with a web server. I would like to Post a run command to start an acquisition. Below are my commands with the error response.
>> s_sch = 'http://webdaq316-ae.local/api/v1.0/schedule/status/';
>> options = weboptions('ContentType','json','MediaType','application/json','RequestMethod','post','ArrayFormat','json')
options =
weboptions with properties:
CharacterEncoding: 'auto'
UserAgent: 'MATLAB 9.5.0.944444 (R2018b)'
Timeout: 5
Username: ''
Password: ''
KeyName: ''
KeyValue: ''
ContentType: 'json'
ContentReader: []
MediaType: 'application/json'
RequestMethod: 'post'
ArrayFormat: 'json'
HeaderFields: []
CertificateFilename: 'C:\Program Files\MATLAB\R2018b\sys\certificates\ca\rootcerts.pem'
>> apikey = 'True'
apikey =
'True'
>> json_command = ['{"run": ', apikey,'}']
json_command =
'{"run": True}'
>> start_sch = webwrite(s_sch,json_command, options)
However, I get the following error.
Error using readContentFromWebService (line 46)
The server returned the status 415 with message "" in response to the request to URL
http://webdaq316-ae.local/api/v1.0/schedule/status/.
Error in webwrite (line 139)
[varargout{1:nargout}] = readContentFromWebService(connection, options);
  1 件のコメント
Fausto Soares
Fausto Soares 2019 年 10 月 24 日
If I start the acquisition via the web server, then I can see the POST (using Google Chrome's Developer Tools option), which contains the 'Request Payload' with the code:
{run: true}
I included these parameters in my 'json_command' argument. Is that the correct way?

サインインしてコメントする。

回答 (2 件)

Neeraj Rajpurohit
Neeraj Rajpurohit 2020 年 6 月 30 日
編集済み: Neeraj Rajpurohit 2020 年 6 月 30 日
The error code here is 415, which means unsupported media type. The server is not being able to identify the request type. I see that you have added 'Arraytype' as 'json'. Instead, I suggest you to 'MediaType' attribute as 'application/json'. See the below example for reference. I am also attaching a link to the document. Hope it helps!
weboptions('MediaType','application/json');
Thanks,
Neeraj

Mudabbir Bhatti
Mudabbir Bhatti 2022 年 7 月 18 日
Hi Fausto,
I am having a similar problem. If you were able to solve this issue, please help me.
Thanks,
Bhatti

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by