Issue changing state via NodeJs

Started by silent__rain__539

silent__rain__539

I’m writing a custom skill for Alexa using AWS Lambda. I’m using NodeJs Http.Request to PUT the value for the On state, effectively turning the device on or off. Tried and tested in Postman and works great. I’ve been trying using NodeJs and I keep receiving a statusCode of 400 (Bad Request). I thought this was to do with the JSON I’m sending in the body but it’s essentially the same code I’ve used on a variety of other external APIs. The only difference being the port. Either that or does the API require certain encoding etc? I’d really appreciate some help as this is one of my first ever projects involving third party APIs.

Edit:
Calling the API to invoke a GET response works as expected every time it is called, using the same URL & Auth Key as the PUT request. I can also use CURL to easily invoke the PUT request from OS X terminal. It is only when using the http.request NodeJS function with the PUT method that it fails every time, returning status of 400. Here is a snippet of my simplified code.

var http = require('http');

var options = 
{
	hostname: nanoleafAPI, // IP Address
 		port: 16021,
 		method: 'PUT',
	path: '/api/v1/' + nanoleafUserKey + '/state/on'
};

var request = http.request(options, function(response)
{
    var responseString = '';
    
    console.log('StatusCode: ' + response.statusCode);
    
    if(response.statusCode !== 200)
    {
        console.log('Error: Non 200 Code Response');
    }
    
    response.on('data', function(data)
    {
        responseString += data;
    });
    
    response.on('end', function()
    {
        console.log('Complete');
    });
});

request.write('{"value" : true}');

request.end();

Tyler

Hey,

My name is Tyler, and I am apart of the Nanoleaf Engineering team, I am the one that wrote the Alexa skill for Nanoleaf, as well. I want to clarify a couple things here.

Amazon Alexa Skill requires you to expose your own service to the public internet, otherwise their voice command module cant issue the command to the specific devices in question. The Open API is designed to operate on the local network only, If you somehow manage to get the Aurora to broadcast on the open network, the only way the Alexa skill would even work is for you to hardcode these responses directly into the Lambda Function itself, otherwise, you would need to run your own translator service in the middle to issue out the command to your local network.

as this issue with Nodejs, I'd step away from using the native http module, and use an abstracted lib, like the request lib, or request promise, or even axios. It will make your life much easier.