Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

HTTP API send Json values fails
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

HTTP API send Json values fails

by Hannes_ on Wed Sep 16, 2020 10:50 am

I am trying to implement a .Net library for the API to adafruit io (Which for now is HTTP only).
It works fine, except when sending .Json as the data in 'value'. According to this "https://io.adafruit.com/api/docs/cookbook.html?ruby#double-encoded-json-strings" i can just send the value as .Json inside the .Json with 'value' 'lat' like this:
{"value":{"name":"Testname"}, "lat":12}
However i will get back a Bad Formatted or Unprocessable Entity from the Server. Even if i try the example provided on the mentioned page it does not work.
Does someone know if it is possible to send the value as .json in the way i am trying to do and it is just a bug in my code or is it maybe not possible with the HTTP API?

If i use the same code to send values of type string {"value":"TestValue", "lat":0.00, ...} it does work, so i would think that all the other settings should be correct.
My current workaround is therefore to send all data as value of type string like that:
{"value":"{\"TestValue\":10}", "lat":0.00, ...} (with escaped "), which does work but requires some extra handling which seems a bit unnecessary.
Does anyone have some information regarding this or spottet an error in the way i try to do that?

Hannes_
 
Posts: 2
Joined: Wed May 27, 2020 12:22 pm

Re: HTTP API send Json values fails

by lorennorman on Fri Sep 18, 2020 11:45 am

Hello Hannes_!

Edit: Oops, I got this wrong! The Adafruit IO API should in fact accept the raw object format as well as a double-encoded string. It is not clear what the issue is, here. The remainder of this response should be disregarded.

It looks like you've explained the behavior accurately, this:
Code: Select all | TOGGLE FULL SIZE
{"value":{"name":"Testname"}, "lat":12}

...will not work, while this:
Code: Select all | TOGGLE FULL SIZE
{"value":"{\"TestValue\":10}", "lat":0.00}

...works fine.

This is because when sending JSON, you do have to encode the "value" field so that it is sent as a string. But you don't have to do this manually, .NET will have a utility function to do the encoding work for you. A quick web search on the subject shows that you can use the `JsonSerializer.Serialize` method for this: https://docs.microsoft.com/en-us/dotnet ... son-how-to

Let me know if this is making sense, and good luck on your .NET api library!
Last edited by lorennorman on Fri Sep 18, 2020 12:40 pm, edited 1 time in total.

lorennorman
 
Posts: 2
Joined: Wed Mar 04, 2020 12:54 pm

Re: HTTP API send Json values fails

by Hannes_ on Fri Sep 18, 2020 12:06 pm

Hello lorennorman,
thanks you for the reply. I just thought that the first version would also work because a similar thing is described in the documentation https://io.adafruit.com/api/docs/cookbook.html?ruby#io-formatted-json:

Code: Select all | TOGGLE FULL SIZE
{
  "value": {"sensor-1":22.587,"sensor-2":13.182},
  "lat": 38.1123,
  "lon": -91.2325,
  "ele": 112
}

Am i reading the document the wrong way or is it maybe a mistake?


Currently i use the second version with newtonsoft .json .Net to handle the .Json de- and encoding.
I wasn't aware that there is the System.Text.Json so thanks for your tip. Are there any advantages you think i should consider?

PS:
If anybody is interested in the project I decided to publish it on github so feel free to use it or participate:
https://github.com/HannesZeihsel/adafruit-io-api

Hannes_
 
Posts: 2
Joined: Wed May 27, 2020 12:22 pm

Re: HTTP API send Json values fails

by lorennorman on Fri Sep 18, 2020 12:36 pm

Oops! I see what you're referencing now, and you're totally right, the raw object form should work as well! We double-checked our side and everything looks fine, so I think we don't have enough information to troubleshoot it further. It sounds like you've gotten it working by double-encoding though, and that's the safest option regardless (particularly for a library!)

As for the appropriate .NET JSON libraries, I am out of my element. I would use whatever the community is using and finding productive, and it looks like that answer was in fact Newtonsoft, at least until MS built something in. I suppose you might decide to use the built-in just to reduce dependencies, but either appear to be fine options.

Thank you so much for open sourcing your work!

lorennorman
 
Posts: 2
Joined: Wed Mar 04, 2020 12:54 pm

Please be positive and constructive with your questions and comments.