Are there any workarounds to be able to receive a JSON payload on the rest api?
Im restricted to recieving JSON and the POST method so my plan was to update a string with it and then extract what i need, however the api only receives text/plain.
Are there any workarounds to be able to receive a JSON payload on the rest api?
Im restricted to recieving JSON and the POST method so my plan was to update a string with it and then extract what i need, however the api only receives text/plain.
I think all the REST API outputs is JSON. I believe the requests input are JSON too.
Yeah I think Bruce is correct
JSON is just a formal for plain text. The text/plain and text/json is a way to tell the receiver what format the text is in so it knows how to deal with it. But in this case:
So just poat the JSON as text/plain if you need to. It will still work.
Hi guys
So using postman I can successfully set a string if I post using content-type : plain/text
Ie: post https://myopenhab.org/items/ItemName
(Header to authenticate)
If I post using the json content-type it does not update the string.
The documentation also says plain text for post item updates
If you Iâm missing something please help and show me an example
Thanks in advance
So just post it using plain/text. Youâve already shown that it works.
Iâve posted as plain text to test, the provider only sends as application/json tho
+1 to accepting text/json in addition to text/plain. It seems odd to me that openhab has support for working with json data, but wonât accept it via the REST API.
Iâd really like to integrate openhab with the vegetronix sensor hub. However it (correctly) labels its report as JSON.
Iâd be happy to try and open a pull request if someone could give me a pointer to where this is implemented.
I think it is likely part of the core. t might be better to file an issue here to be included in openHAB 3. There are no more core changes for openHAB 2.x.
But what would you do with it? There is no JSON Item type to accept it.
I was hoping I could just store it as a String
, and then use rules + transform("JSONPATH", ...)
to parse it and put the results into other Items.
Thanks for the pointer!
Yep, that was kind of my point. Itâs just a string youâre sending to openHAB, and you want it to be treated as just a string. Itâs almost an âabuse of processâ to want to send something marked as âjsonâ but please treat it as âtextâ.
However convenient it might be for your particular problem, it doesnât sound appropriate (though of course thatâs not my decision).
The âcorrectâ approach for getting JSON from some remote service and analyzing into multiple Items would normally be a binding.
Actually Python & PHP do that quite easily.
Python, PHP, JavaScript, and even Java do it quite easily. But what rossko57 is referring to is how OH is architected and implemented. I tend to agree with him that it feels a bit odd to accept JSON into a plain old String Item. And since OH doesnât have a JSON type Item, a plain old String Item is all we got.
But I also see Michaelâs point because that means that the integration he is after isnât possible.
So as far as I can see, the solution would be either:
Sounds like something for the OH3 roadmap
If the maintainers think itâs something appropriate to do. Just because something can be done doesnât mean it should be done.
And, I suspect, unless it is listed ad a GitHub issue it would not even be considered.
Iâm not sure I understand this concern. The magic of JSON is that it is a human readable String that can also be interpreted by computers. I think the situation would be different were this some binary format that could be corrupted by interpreting it as text. In that case I would agree with blocking it.
If it is improper to store JSON in a String, then is the transform function provided by openhab (which can take in a string and interpret it as JSON) also improper?
Should I open up an issue on github then? (Sorry, Iâm very new to this community.) @rlkoshak 's proposal of accepting all text mime types would definitely solve my use case.
I believe this is the proper place.
Not at all. My concern is not about shovelling JSON content around in strings, of course that happens in just about any system, whether for convenience or necessity.
Itâs about sending an http message that is marked as JSON to a well-defined service, and expecting that service to mishandle the JSON as though it were marked as text.
Thereâs no imperative for doing that, and it would be reasonable to resist it on the basis that it would adversely impact future development of the REST API. itâs not far-fetched to suppose some future enhancement that does use JSON properly - say, updating both state and tags, or updating multiple Items.
The REST API is quite specific to openHAB. While it is a tool for users, this use case here feels like it is being pressed into service to suit some more general requirement.
What is ideally needed for this job is an âHTTP Serverâ binding that could service unsolicited HTTP payloads, be they JSON or XML or what, and the user can (as with other bindings) use transformation techniques to pre-process, allot the data to several Items, and/or screen out rubbish, as they wish.
It would be generally useful, I think.
The closest existing thing is the TCP listening binding, which could be used here, but would require a fair amount of building onto I think.