1

I'm building an alexa skill using the new SDK 2.0 and I'm now having trouble implementing a simple http get request. How would I add an authorization header to the getRemoteData url request? The code below isn't working.

I'm trying to call Airtable API to get some remote data

const UserReplyIntent_Handler =  {
canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest' && request.intent.name === 'UserReplyIntent' ;
},
async handle(handlerInput) {
    const response = await httpGet();

    console.log(response);

    return handlerInput.responseBuilder
            .speak("Okay. Here is what I got back." + response.records.fields.Indication)
            .reprompt("Would you like to learn more?")
            .getResponse();
},
};     
function httpGet() {
return new Promise(((resolve, reject) => {
    const headers = {
        Authorization: 'Bearer key12345678'
    };
    var options = {
        host: 'api.airtable.com',
        port: 443,
        path: '/v0/appYqfJ3Rt2F0sRGn/Database?filterByFormula=(DrugName=%27azatadine%27)',
        method: 'GET',
    };
    const request = https.request(options, {headers}, (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();   
}));
}

1 Answers1

3

header go into the options object, not as a separate parameter:

    var options = {
        host: 'api.airtable.com',
        port: 443,
        path: '/v0/appYqfJ3Rt2F0sRGn/Database?filterByFormula=(DrugName=%27azatadine%27)',
        method: 'GET',
        headers: {
          Authorization: 'Bearer key12345678'
        }
    };

https.request accepts the same options fields as http.reqest. http.request options object allows headers to be defined.

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
  • Ok fixed but my response is coming back as object object. Not sure how to stringify it into json or what I should do? – notyourguru Jul 20 '19 at 11:03