同时使用“ Alexa Skill Kit”(AWS Lambda nodejs环境)在Alexa自定义技能中使用nodejs消费REST API POST请求。 这是我在index.js中的代码
'use strict';
const Alexa = require('alexa-sdk');
const request = require('sync-request');
'validateClientIntent': function() {
var json = {
"clientID": "xxxxx6EF-9D05-49CE-855C-C41CED4A83B5",
"appKey": "D/xxxxxxxxisFSMMzAbntGsrNf0yX6aafNqoyi8="
};
var options = {
url: 'http://link',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: json
};
//var method = "POST";
//options = JSON.parse(options);
try {
request(options, function(err, res, body) {
if (res && (res.statusCode === 200 || res.statusCode ===
201)) {
//var body = JSON.parse(body);
var trackingId = body;
//console.log(trackingId);
//return trackingId;
this.response.speak(trackingId);
}
else {
throw (err);
}
});
}
catch (err) {
this.response.speak(err);
}
this.emit(':responseReady');
},
答案 0 :(得分:0)
在此,我正在使用“ sync-request”,因此请使用下面提供的方法 'https://www.npmjs.com/package/sync-request'
var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
json: {username: 'ForbesLindesay'},
});
var user = JSON.parse(res.getBody('utf8'));