简体   繁体   English

HTTP发布请求未在AWS Lambda中通过

[英]HTTP Post request not going through in AWS Lambda

I am trying to add an item to my Todoist project through an Alexa skill in AWS Lambda. 我正在尝试通过AWS Lambda中的Alexa技能向我的Todoist项目添加项目。 I am very new to all of these technologies so forgive me if the fix is incredibly obvious. 我对所有这些技术都非常陌生,因此,如果修复非常明显,请原谅我。 When I ask Alexa to invoke my addZipcode skill, it fails. 当我要求Alexa调用我的addZipcode技能时,它失败了。 This is what I have (excluding some stuff that is in all Alexa Lambda functions): 这就是我所拥有的(不包括所有Alexa Lambda函数中的某些东西):

Alexa stuff
...
const handlers = {
'LaunchRequest': function() {
    this.emit('AMAZON.HelpIntent');
},
'addZipcode': function() {

    const newZip = this.event.request.intent.slots.zipcode.value;
    const speechOutput = newZip;

    var http = require("https");
    function postZip(newZip) {
        var options = {
            "method": "POST",
            "hostname": [
                "beta",
                "todoist",
                "com"
            ],
            "path": [
                "API",
                "v8",
                "tasks"
            ],
            "headers": {
                "Content-Type": "application/json",
                "Authorization": "Bearer " + token
            }
        };

        var req = http.request(options, function(res) {
            var chunks = [];
            res.on("data", function(chunk) {
                chunks.push(chunk);
            });

            res.on("end", function() {
                var body = Buffer.concat(chunks);
                console.log(body.toString());
            });
        });
        req.write(JSON.stringify({ content: newZip, project_id: XXXXXXXXXX }));
        req.end();
    }

    postZip(newZip);

    this.response.cardRenderer(SKILL_NAME, newZip);
    this.response.speak(speechOutput);
    this.emit(':responseReady');

},
.... cont

I get the resulting error when I try to run the skill with Alexa: 当我尝试使用Alexa运行该技能时,我得到了结果错误:

Response:
{
  "errorMessage": "hostHeader.replace is not a function",
  "errorType": "TypeError",
  "stackTrace": [
    "Agent.addRequest (_http_agent.js:130:39)",
    "new ClientRequest (_http_client.js:159:16)",
    "Object.exports.request (http.js:31:10)",
    "Object.exports.request (https.js:199:15)",
    "postZip (/var/task/index.js:72:28)",
    "Object.addZipcode (/var/task/index.js:88:9)",
    "emitNone (events.js:86:13)",
    "AlexaRequestEmitter.emit (events.js:185:7)",
    "AlexaRequestEmitter.EmitEvent (/var/task/node_modules/alexa-sdk/lib/alexa.js:216:10)",
    "AlexaRequestEmitter.ValidateRequest (/var/task/node_modules/alexa-sdk/lib/alexa.js:181:23)"
  ]
}

I tried searching for more information about hostHeader.replace or even just hostHeader but to no avail. 我试图搜索有关hostHeader.replace或什至只是hostHeader的更多信息,但无济于事。 When I surround my postZip function with 当我包围我的postZip函数时

exports.handler = function(event, context, callback) {} 

the skill actually works, but the Post request does not go through (as in, the new zipcode is not added as a new task on my Todoist). 该技能确实有效,但是Post请求没有通过(例如,新的邮政编码未作为新任务添加到Todoist上)。 I'm pretty sure the Post request code itself is correct because I ran it through Postman and the zipcode was added. 我非常确定Post请求代码本身是正确的,因为我是通过Postman来运行它的,并添加了邮政编码。

Please help me understand why it doesn't work. 请帮助我了解为什么它不起作用。

It's hard to tell what causes that error. 很难说出导致该错误的原因。 But the node docs say, that hostname as well as path are supposed to be nothing but strings and not arrays as it's the case in your code. 但是节点文档说, hostnamepath应该是字符串,而不是数组,而在代码中就是这种情况。

So what I'd do first is to change your code to this: 因此,我首先要做的就是将您的代码更改为此:

var options = {
        "method": "POST",
        "hostname": "beta.todoist.com",
        "path": "/API/v8/tasks",
        "headers": {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + token
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM