 |
Takilara Apprentice

Joined: 31 Jan 2002 Posts: 129 Location: Norway
|
Posted: Sun Mar 08, 2020 7:24 pm
Set Content-Type: application/json with %url? or VBscript solution to post JSON |
Hello,
I am trying to have CMUD (Pro) upload telemetry to a Thingsboard page.
I currently do this by having CMUD write out CSV files, and an external script monitoring these files, and push the telemetry to Thingsboard.
I would like to do this inside CMUD itself, and saw the %url function, and gave this a try.
I would like to be able to set the Content-Type header, but i cant seem to be able to get this to work. Seems regardless of what i do, the Content Type seems to be sent as application/x-www-form-urlencoded.
Is there a way to change the Header when using %url?
I have some other possible options lined up, and thoughts on these will also be welcome.
Option 1: Properly set Content-Type header on %url object, no external scripts or code required
Option 2: Somehow do this with VBScript or LUA
- Would appreciate help on how to pass variables as parameters to a #FUNCTION defined as VBScript. (I have figured out how to get the VBScript to access CMUD variables by using the zvar, but i don't like the global nature of that)
- I think this might be easier to do with #SCRIPT + %mss, at least then i seem to be able to pass arguments to the VBScript function, something i havent been able to figure out for #FUNCTION defined as VBScript
Option 3: Setup a lightweight singlepurpose webservice that responds to CMUD's %url.POST (application/x-www-form-urlencoded), properly encodes, and forwards as application/json to Thingsboard
All opinions are welcome:) |
|
_________________
 |
|
|
 |
Takilara Apprentice

Joined: 31 Jan 2002 Posts: 129 Location: Norway
|
Posted: Sun Mar 08, 2020 9:31 pm |
Note that i have gotten this to work with JScript and WScript using zvar helper variable. JScript seem by far the cleanest.
(I am still curious about the %url function though...)
I put the below as an alias, using JScript
Code: |
var token = "xxxxxxxxxxx";
$host = "http://127.0.0.1"
var port = 8080;
var telemetryUrl = host + ":" + port + "/api/v1/" + token + "/telemetry";
//construct json
//var xp = zvar.xp;
//var xpcost = zvar.xpcost;
// In CMUD, store the dbvar to a string var using
// #var js %json(dbvar)
// Then collect it in JScript by using
// var myvar = zvar.js
var testvar = zvar.js;
// for debug things, instanciate a WScript.Shell, then use shell.Popup()
//var shell = new ActiveXObject("WScript.Shell");
//shell.Popup(testvar);
var strJSONToSend = testvar;
xhr = new ActiveXObject("Microsoft.XMLHTTP");
xhr.open("POST", telemetryUrl, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
//var json = JSON.parse(xhr.responseText);
//console.log(json.email + ", " + json.name);
}
}
xhr.send(strJSONToSend);
|
|
|
_________________
 |
|
|
 |
shalimar GURU

Joined: 04 Aug 2002 Posts: 4754 Location: Pensacola, FL, USA
|
Posted: Mon Mar 09, 2020 5:24 pm |
Hmm.. never got around to using %url myself, lots of options.
Try this?
$url=%url("http://127.0.0.1")
$url.Port=8080
$url.ContentType="some/string"
$post=$url.Post
#SHOW $post |
|
_________________ Discord: Shalimarwildcat |
|
|
 |
Takilara Apprentice

Joined: 31 Jan 2002 Posts: 129 Location: Norway
|
Posted: Mon Mar 09, 2020 7:22 pm |
shalimar wrote: |
Hmm.. never got around to using %url myself, lots of options.
Try this?
$url=%url("http://127.0.0.1")
$url.Port=8080
$url.ContentType="some/string"
$post=$url.Post
#SHOW $post |
Thanks, It feels like that property is read only though.
$url.ContentType="application/json" give me "Access violation at address 0000000", while if i use
$url.Content-Type="application/json" I get "No method/property name: Content-type"
So apparently there is a method or property for ContentType. If i try to use it as ContentType("application/json"), I get illegal property.
Running the quoted script in the alias unmodified give the same Access violation
I suspect that the ContentType is only to read the return message? (indeed, it returns text/html on the server response when running towards a standard webserver)
I think it might be that i have to set this somehow in the Params or ParamStr properties...
(both of these are empty, i guess i can try some various permutations of contenttype="application/json" in those...) |
|
_________________
 |
|
|
 |
shalimar GURU

Joined: 04 Aug 2002 Posts: 4754 Location: Pensacola, FL, USA
|
Posted: Mon Mar 09, 2020 10:01 pm |
I would get to trial and error with ample use of #SAY to see what is what.
|
|
_________________ Discord: Shalimarwildcat |
|
|
 |
Takilara Apprentice

Joined: 31 Jan 2002 Posts: 129 Location: Norway
|
Posted: Thu Mar 12, 2020 10:39 pm |
shalimar wrote: |
I would get to trial and error with ample use of #SAY to see what is what. |
I gave up on the %url
I would assume it is possible to get it to what i wanted to do, however, since i had the working (almost) Jscript, i went down that path.
For info though:
- using zvar.xxx in a JScript passes by reference, not by value, often borking the ZScript side of the variable. (in particular, i was using %json() to store the json string to a variable. After this getting passed to the JScript, the quotes in the JSON would be gone (this can happen even without the JScript also). I got this to work better by using zvar.xxxx.slice(0) to copy the variable
- Due to some various pieces of weirdness in the %json method (I could not get it to work consistently for more than 10-20 times before it would start stripping quotes). I ended up writing my own #FUNC json, this works ok as long as it is a flat structure
 |
|
_________________
 |
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|