Rest api - able to get propertys of web things but not to set

I would like to control web things from a web page.
I’m able to get property this is my code:
var url = “https://***.mozilla-iot.org/things/gpio-20/properties/on”;
try {
var mzreq = new XMLHttpRequest();
mzreq .open(“GET”, url, false);
mzreq .setRequestHeader(‘Accept’, ‘application/json’);
mzreq .setRequestHeader(‘Authorization’, ‘Bearer eyJ…’);
mzreq .onreadystatechange = function () {
if (mzreq .readyState === 4) {
if (mzreq .status === 200 || mzreq .status == 0) {
var allText = mzreq .responseText;
alert(allText);
}
}
}
mzreq .send(null);
}
catch (err) {
alert(err);
}

but i’m not able to set property value:
var url = “https://***.mozilla-iot.org/things/gpio-20/properties/on”;
try {
var mzreq = new XMLHttpRequest();
mzreq .open(“PUT”, file, true);
mzreq .setRequestHeader(‘Accept’, ‘application/json’);
mzreq .setRequestHeader(‘Authorization’, ‘Bearer eyJ…’);
mzreq .onreadystatechange = function () {
if (mzreq .readyState === 4) {
if (mzreq .status === 200 || mzreq .status == 0) {
var allText = mzreq .responseText;
alert(allText);
}
}
}
mzreq .send(’{ “on”: true }’);
}
catch (err) {
alert(err);
}

I get response 400.

P.S.:
I have installed gateway manually from github, not from pre-built image.

I believe the bug here is that you need to set the PUT XMLHttpRequest’s ‘Content-Type’ header to ‘application/json’

1 Like

Thank you James, now it work.
Another question:
How can I control things on local network?
I have test https://ip:4443, it work via browser but not with XMLHttpRequest

Man, you saved my day ^^ It took me 3 Hours to figure this out. Thank you so much.