Fetch with POST gives 'commands param is null'

Discussing technical aspects of Yun development products, including IoT Core and other cloud service APIs, data analysis products, etc.


Post Reply
Marty McFly
Posts: 7

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;
}
Marty McFly
Posts: 7

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

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);
});
Marty McFly
Posts: 7

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

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.

JamesG
Posts: 38

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

Are you able to operate normally now?

Marty McFly
Posts: 7

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

JamesG 2023年 Dec 17日 20:10

Are you able to operate normally now?

Yes James. Thank you very much!

Post Reply