0

JSON parsing

Krzysztof Szczepański 9 months ago in Devices / Basic updated by Krzysztof Szczepański 7 months ago 13

I created a TcpServer with a JSON parser:

Image 4625

I added a string parser with a token :

Image 4627


I use Postman and send :

{"TestData":{"Data1":"1"}}


ComfortClick receives data:

POST / HTTP/1.1

Content-Type: text/plain

User-Agent: PostmanRuntime/7.32.3

Accept: */*

Postman-Token: e79d7b2e-8a0a-4ef6-bd47-8d5cc7e8c6f3

Host: 192.168.1.7:16900

Accept-Encoding: gzip, deflate, br

Connection: keep-alive

Content-Length: 26


{"TestData":{"Data1":"1"}}


But the parser throws an error:

Devices : 15.08.2023 14:54:09 : Error : Devices\Test\String : Error parsing received data. Unexpected character encountered while parsing value: P. Path , line 0, position 0. w Newtonsoft.Json.JsonTextReader.ParseValue()

w Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)

w Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)

w ComfortClick.Tasks.Common.ReceiverBase.OnDataReceived(String data)


It looks like the header is being passed to the parser, not the JSON itself.

Bug or my configuration error?

How to disable header passing to parser?

Tested on ComfortClick v 4.9.36

Hey

Try if: .TestData.Data1 works. Note the dot in front.

Image 4628


not working :( same error.

Devices : 15.08.2023 16:37:59 : Error : Devices\Test\String : Error parsing received data. Unexpected character encountered while parsing value: P. Path , line 0, position 0. w Newtonsoft.Json.JsonTextReader.ParseValue()

Have you tried different parser encodings too? Set it to none.

Image 4629


not working :( same error. ( with and without dot in front ) 

Devices : 15.08.2023 20:48:14 : Error : Devices\Test\String : Error parsing received data. Unexpected character encountered while parsing value: P. Path , line 0, position 0. w Newtonsoft.Json.JsonTextReader.ParseValue()

Well then im out of ideas. I made a similar test with postman and it works for me. Http-postman mock api test.bos

Maybe it works because you are using a different device in Comfortclick. I use Basic, you use HTTP :)

Image 4631

my test file : Basic-Test.bos

Test (demo).bos

Here is another demo for you - used udp sender and receiver with basic driver.

Does this work for you then? 

I know i used http - i wasnt sure why you used basic driver in the first place. Not sure tcp server works like that, but http does work.

I am supposed to receive POST information in JSON from an external system. They make a blind call to the address and port I give them. I have the key in the header and the rest of the information in JSON.

I got around the decoding with regex, but the next issue came up with HTTPS. Comfortclick returns encoded information and I don't know how to get around it :(

Well you need to wait for CC support then. I tested the basic tcpserver and i got no response from it either. 
If you have a set ip for it does http driver get any values from it?

I have everything configured as in the file I exported. Postman is installed locally and when sending http, data reaches Comfortclick without any problems, both in http and https.

This is what it looks like:

http:

Image 4636

https:

Image 4635

Now I have two problems:
1. JSON parsing doesn't work - probably due to passing header to parser, workaround is regex
2. TCPServer does not decrypt https connections

Thanks for your help, but I guess the problems lie deeper in CC.
I am waiting for their support :)

I now got as far you did and im facing same problems - http works but https does not. Strange that for me even the regex parser isnt working - i can get the correct response, thought i could parse it with regex so only the json value remains but it doesnt want to parse its values even if i send it with a program. If i turn off all headers and send it as post i still get: POST / HTTP/1.1 {"TestData":{"Data1":"6"}} and the first part is messing up the json parser.

If you can modify the postman mock api server then that works for me as much as i tested it. Its able to send the value without any headers and then the json parser works for me. If postman runs as a service and mirrors the received value to mock server then it should work hopefully.  Something like this: https://learning.postman.com/docs/sending-requests/capturing-request-data/capturing-http-requests/

here you have a working regex parser :test-regex.bos

(?<={\"TestData\":{\"Data1\":")\d+

Currently, the most important thing for me is to make https work.

I can bypass the broken JSON parser.

CC Support: Knock... Knock... :)

Reply from CC :

Regarding the issue you're facing, the TCPserver receives raw data, so you can only use http connection. Incrypted https connection will not work and you will get those strange symbols that you're experiencing.

If you want to use https and encrypted data, use our RPC connection instead of TCPserver.