How to use RPC protocol (Remote Procedure Call) to send a HTTP request as formatted JSON to a web server?
30 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
rasoul rad
il 13 Gen 2017
Commentato: rasoul rad
il 20 Gen 2017
I have a instrument that capture a set of data and works as a web service with IP address 172.20.32.192. I want to get some data from this instrument and I'm trying to communicate via Remote Procedure Call protocol (RPC protocol) because this device supports only RPC protocol. In the user manual of it, this expressed that the data exchange format is the JSON (JavaScript Object Notation). To communication, I must send a request via HTTP POST in the body of the HTTP request as a JSON object. The URL to do this is http://IP Address/rpc. According to the user manual contents, an example for request and response must be following:
request = {
"version": "1.0",
"proc": "GetPlantOverview",
"id": "1",
"format": "JSON"
}
response = {
"version": "1.0",
"proc": "GetPlantOverview",
"id": "1",
"result":
{
"overview":
[{
"meta": "GriPwr",
"name": "current power",
"value": "4250",
"unit": "W"
}]}}
I used the webread and webwrite functions with Query name-value pairs or Post name-value pairs like following:
options = weboptions('RequestMethod','post','ArrayFormat','json','Timeout',10);
resp = webread('http://172.20.32.192/rpc','version','1.0','proc','GetPlantOverview','id','1','format','json',options) % or using webwrite
but i got only HTML page that it wasn't like the response. How can i send a HTTP request as formatted JSON via RPC protocol to get data from that instrument? I'll be grateful for helps.
0 Commenti
Risposta accettata
Robert Snoeberger
il 17 Gen 2017
Modificato: Robert Snoeberger
il 17 Gen 2017
The documentation for webread says that it only supports application/x-www-form-urlencoded for HTTP POST requests [1]. Since you are trying to send application/json, you need to use webwrite. I think the following should do what you want.
>> body.version = '1.0';
>> body.proc = 'GetPlantOverview';
>> body.id = '1';
>> body.format = 'JSON';
>> options = weboptions('MediaType', 'application/json');
>> resp = webwrite('http://172.20.32.192/rpc', body, options)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su JSON Format in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!