Page 1 of 1

Fetch with POST gives 'commands param is null'

Posted: 2023年 Dec 17日 06:18
by Marty McFly

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;
}

Re: Fetch with POST gives 'commands param is null'

Posted: 2023年 Dec 17日 06:29
by Marty McFly

I just tried it a different way and now I get 'sign invalid'

Code: Select all

var method2 = "POST";
var timestamp2 = Date.now().toString();
var signUrl2 = '/v1.0/devices/xxxxxxxxxx/commands';
var contentHash2 = CryptoJS.SHA256(signStr2, secretKey);
var stringToSign2 = [method2, contentHash2, '', signUrl2].join('\n');
var signStr2 = ClientID + token + timestamp2 + stringToSign2;

var settings = {
  "url": "https://openapi.tuyaus.com//v1.0/devices/xxxxxxxxxx/commands",
  "method": "POST",
  "timeout": 0,
  "headers": {
    "client_id": ClientID,
    "access_token": token,
    "sign": await calcSign(signStr2, secretKey),
    "t": timestamp2,
    "sign_method": "HMAC-SHA256",
    "Content-Type": "application/json"
  },
  "data": JSON.stringify({
    "commands": [
      {
        "code": "switch_1",
        "value": true
      }
    ]
  }),
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

Re: Fetch with POST gives 'commands param is null'

Posted: 2023年 Dec 17日 10:13
by Marty McFly

I figured it out. A couple of things I was not doing...
The body....

Code: Select all

{
  "commands": [
    {
      "code": "switch_led",
      "value": true
    }
  ]
}

needs to be converted to JSON....

Code: Select all

{"commands":[{"code":"switch_led","value":true}]}

and then encrypted with SHA256.


Re: Fetch with POST gives 'commands param is null'

Posted: 2023年 Dec 17日 20:10
by JamesG

Are you able to operate normally now?


Re: Fetch with POST gives 'commands param is null'

Posted: 2023年 Dec 18日 10:29
by Marty McFly
JamesG 2023年 Dec 17日 20:10

Are you able to operate normally now?

Yes James. Thank you very much!