I think this might just be syntax related.
I have no problem getting a token and then getting a device response with any GET command.
I am trying a fetch with POST to send a command to a device and I am getting "commands param is null"
Hope this is something simple that someone may be able to help with?
Code: Select all
var method2 = "POST";
var timestamp2 = Date.now().toString();
var signUrl2 = '/v1.0/devices/xxxxxxxxxxxxx/commands';
var contentHash2 = CryptoJS.SHA256(signStr2, secretKey);
var stringToSign2 = [method2, contentHash2, '', signUrl2].join('\n');
var signStr2 = ClientID + token + timestamp2 + stringToSign2;
fetch('https://openapi.tuyaus.com//v1.0/devices/xxxxxxxxxxxxx/commands', {
method: 'POST',
data: JSON.stringify({
"commands": [
{
"code": "switch_1",
"value": true
}
]
}),
headers: {
't': timestamp2,
'sign_method': 'HMAC-SHA256',
'Content-Type': 'application/json',
'client_id': ClientID,
'sign': await calcSign(signStr2, secretKey),
'access_token': token
},
}).then(response => response.json())
.then((data) => {console.log(data)});
}
async function calcSign(signStr2, secretKey){
var hash2 = CryptoJS.HmacSHA256(signStr2, secretKey);
var hashInBase642 = hash2.toString().toUpperCase();
return hashInBase642;
}