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();