为什么我必须手动转义我的引号以便http.request。 write方法发送JSON

问题描述 投票:0回答:1

执行下面的代码时,我可以通过接缝来获取request.write()的唯一方法是手动转义引号。我已经尝试过JSON.stringify()这个论点,然后传入它并且仍然没有缝合工作。

关于这里发生了什么以及如何解决它的任何想法?

我尝试过JSON.stringify(message)以及其他一些方法。我也开始通过香草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();
}

上面的代码集成了松弛。

当发送request.write(message)时,我希望松弛输出我的消息。

相反,我留下的是一个空白的回应。好像没有出错但没有数据发送。

node.js slack
1个回答
0
投票

对于我如何将返回消息格式化为松弛,这是一个简单的问题。

我需要确保我的松弛块首先被添加到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();
}

© www.soinside.com 2019 - 2024. All rights reserved.