Http Post Request in AWS Lambda

I am trying to make a post request to particle via AWS Lambda

function httpCall() {
  return new Promise(((resolve, reject) => {
    var options = {
        host: 'https://api.particle.io',
        port: 443,
        path: '/v1/devices/RandomDeviceId/caller',
        method: 'POST',
        json: {"access_token":"RandomAccessToken","args":"smart"}
    };
    
    const request = http.request(options, (response) => {
      response.setEncoding('utf8');
      let returnData = '';

      response.on('data', (chunk) => {
        returnData += chunk;
      });

      response.on('end', () => {
        resolve(JSON.parse(returnData));
      });

      response.on('error', (error) => {
        reject(error);
      });
    });
    request.end();
  }));
}

/********************************************************************************/
/********************************************************************************/
const LightIntent ={
    canHandle(handlerInput){
        return handlerInput.requestEnvelope.request.type === 'IntentRequest' && 
        handlerInput.requestEnvelope.request.intent.name === "LightIntent";
    },
    async handle(handlerInput) {
        let speakOutput = "Defaul message"  
        const response = await httpCall();
        
        console.log(response);
        speakOutput = "OK";
            return speakOutput;
        }
};

However, the response that i am getting is the following:

I was hoping someone could point me in the right direction on what i am doing wrong while making this call?

Thanks

I changed the host from

host: 'https://api.particle.io',

to

host: 'api.particle.io'

and now i am getting the following error:

image

would really appreciate if someone could point out what the issue :slight_smile:

Can you try hostname: 'api.particle.io' and call https.request() instead of http.request()?

Hey @ScruffR ! Hope you are doing well.

So the Http is actually Https, sorry for the confusion:

var http = require('https');

Any other recommendations?

Hi, what if you compare with this cloud function tool?

Seems like it’s getting a 403 since the request not include the Authorization: Bearer header.


Note: I realize this is a non-working example in my picture, however, you can adjust this in your case to make it work.
Cheers,
Gustavo.

I believe that the problem is that you can’t put the access_token in a json encoded POST or PUT body. It really only works for application/x-www-form-urlencoded. You should put the token in the Authorization header instead.

1 Like

Hi, you can try something like this:

function httpCall() {
  return new Promise(((resolve, reject) => {
    var options = {
        host: 'api.particle.io',
        port: 443,
        path: '/v1/devices/RandomDeviceId/caller',
        method: 'POST',
        mode: 'cors',
        headers: {'Content-Type': 'application/json','authorization':'Bearer ' + YourAccessToken 
         },
        body: JSON.stringify({args:"smart"}) 
    };
    const request = http.request(options, (response) => {
      response.setEncoding('utf8');
      let returnData = '';

      response.on('data', (chunk) => {
        returnData += chunk;
      });

      response.on('end', () => {
        resolve(JSON.parse(returnData));
      });

      response.on('error', (error) => {
        reject(error);
      });
    });
    request.end();
  }));
}

it’s working for me with pure JS and fetch

1 Like