How to use RPC protocol (Remote Procedure Call) to send a HTTP request as formatted JSON to a web server?
30 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
rasoul rad
el 13 de En. de 2017
Comentada: rasoul rad
el 20 de En. de 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 comentarios
Respuesta aceptada
Robert Snoeberger
el 17 de En. de 2017
Editada: Robert Snoeberger
el 17 de En. de 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)
Más respuestas (0)
Ver también
Categorías
Más información sobre JSON Format en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!