为什么我必须手动转义我的引号才能 http.request。写方法发送 JSON

Why do I have to manually escape my quotes in order for http.request. write method to send JSON

当执行下面的代码时,我唯一能让 request.write() 工作的方法是手动转义引号。我在传递参数之前尝试 JSON.stringify() 并且仍然无法正常工作。

关于这里发生的事情以及如何解决它有什么想法吗?

我已经尝试了 JSON.stringify(message) 以及许多其他方法。我也准备通过 vanilla https.request.

来做到这一点
function samplePost(responseURL){
    let postOptions = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }
    };
    let request = https.request(responseURL, postOptions, (res) => {
        let rawData = '';
        res.on('data', (chunk) => { rawData += chunk; });
        res.on("end",() => {
            //console.log(rawData);
        });
    });

    //can't seem to send this along properly unless I escape all quotations
    //example "{ \"text\": \"Testing this message out once again...\" }"
    let message = [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Are you sure you want to invite to :video_game:?\n*" + email + "* on *" + platform + "*"
            }
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "emoji": true,
                        "text": "Invite"
                    },
                    "value": "invite"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "emoji": true,
                        "text": "No"
                    },
                    "value": "dont_invite"
                }
            ]
        }
    ];
    request.write(message);
    request.end();
}

以上代码与slack集成。

我希望 slack 在发送 request.write(message) 时输出我的消息。

相反,我剩下的是空白回复。好像什么都没有发生,但也没有发送数据。

关于我如何将 return 消息格式化为 slack,结果证明这是一个简单的问题。

我需要确保首先将我的松弛块添加到一个 json 对象中,并将 "blocks" 作为我块所在位置的键。其次,在发送之前我需要 JSON.stringify 和 json。见下文。

function samplePost(responseURL){
    let postOptions = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }
    };
    let request = https.request(responseURL, postOptions, (res) => {
        let rawData = '';
        res.on('data', (chunk) => { rawData += chunk; });
        res.on("end", () => { });
    });
    let messageBlocks = [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Are you sure you want to invite to :video_game:?\n*" + email + "* on *" + platform + "*"
            }
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "emoji": true,
                        "text": "Invite"
                    },
                    "value": "invite"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "emoji": true,
                        "text": "No"
                    },
                    "value": "dont_invite"
                }
            ]
        }
    ];
    //ADDED THIS
    let returnMessage = {
        "blocks": messageBlocks
    }
    request.write(JSON.stringify(messageBlocks));
    request.end();
}