LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Communicating to a device over ethernet using JSON

Solved!
Go to solution

Hello
I have an item of equipment on a system and I need to send commands and read data to and from it.
I have been told it uses HTTP/JSON-RPC API.

So I don't know what I don't know here! and I need some 'get out of the box' help if possible.
I have attached a screenshot hopefully which will explain all.
Many thanks in advance
Lee

 

0 Kudos
Message 1 of 8
(2,961 Views)

Hi Lee,

 

If you're confident with the JSON string, you might simplify your initial testing by removing the Flatten to JSON node. Probably in the long run you'll want to use this to allow easier manipulation of the input (rather than having to specify a string) but to get started I'd suggest as few moving parts as possible.

 

After that, I'd try maybe using a POST request rather than PUT + GET. I'm not certain this will help, but I think it might give you something different to look at at least.

The GET result is returning the same as your Chrome browser, because that's the default request type when you browse to a website. Unfortunately, it doesn't appear that the GET type is supported in that URL edit: I'd say more accurately it is supported (because you got something back) but it isn't the method you want (hence the suggestion to try POST). The PUT method appears to be unsupported.


GCentral
0 Kudos
Message 2 of 8
(2,939 Views)

Good morning,

Thank you for your reply and what you describe is along the same lines as the reponse from the manufacturer, see below. So it looks as if it needs some header information, there is a header section of vi's in the palette but i cant see how to include as it looks like i need to use some handle vi's as well? Im still somewhat confused. Many thanks in advance if you can help.

 

Looking at your pictures I think the issue is with the HTTP request contents. We didn't document that as this header information it's normally handled by the libraries used when using a conventional programming language. I will add some information to the document on this but to help now an example HTTP request header with json-rpc body is given below:

 

============================

POST /api1 HTTP/1.1
Host: 192.168.201.46
Content-Type: application/json-rpc
Content-Length: 87

 

{"id": "b972f1d8-3ef4-429f-b5ca-88e42ef95cae", "method": "getStatus", "jsonrpc": "2.0"}

============================

 

So I think the main differences are:

  1. Needs to be a HTTP "POST" request to send the JSON request.
  2. Needs the "Content-Type: application/json-rpc" to specify the content type is json-rpc.
0 Kudos
Message 3 of 8
(2,882 Views)

Hi again,

 

So for the headers, I think that yes - you do need a handle.

You can create one using the HTTP library's "OpenHandle.vi" - note that none of the inputs are required (this initially caught me out).

 

Then you can add headers (there's a dropdown if you Create Constant, but you can type whatever you want there) using the AddHeader.vi.

 

Here's an image of an example (several VIs):

headers.png

You can see that I have a session reference stored in an FGV here (you could create one each time, but I need to store some additional information and it's easier for me to do this just once every now and again) and that I call the FGV with the POST action and a bunch of data from my "Create Payload.vi". This is used in the POST Multipart node, but you could use something simpler if you don't need this.


GCentral
0 Kudos
Message 4 of 8
(2,871 Views)

I seem to get a response now, but unsure if I am correctly setting the header.  A picture tells a thousand words so I have attached a screenshot.

0 Kudos
Message 5 of 8
(2,852 Views)

The screenshot looks reasonable to me.

 


@LeeBowers1 wrote:

I seem to get a response now, but unsure if I am correctly setting the header.  A picture tells a thousand words so I have attached a screenshot.


Is the response what you expect? And of so, does anything appear not to be working?

 

If not, seems like your problem is solved.

 

If there's still some issue/unexpected behaviour, feel free to let me/us know what it is and I/the forum can try and give further advice.


GCentral
0 Kudos
Message 6 of 8
(2,845 Views)

Hello again, thank you for replying

 

These are the results we could get

 

Response when ok:
{"jsonrpc":2.0,"id":0,"result":null}
Response when error:
{“jsonrpc”:”2.0”, “id”: 0, “error”:{“code”: -32601, “message”: “Unknown method”}}

 

as we are getting the error response i suspect we are still not sending the header or something correctly. It is like we are almost there but not quite ...

0 Kudos
Message 7 of 8
(2,842 Views)
Solution
Accepted by topic author LeeBowers1

Hello,

I now have the unit responding correctly. I was incorrectly using the unflatten to json function. Bit of a mixture of sending a string in the json format and then also using the unflatten to json as well!

Many thanks for your helps and replies.
Lee

0 Kudos
Message 8 of 8
(2,830 Views)