【问题标题】:Why is Node.js response JSON displayed in raw form in Slack?为什么 Node.js 响应 JSON 在 Slack 中以原始形式显示?
【发布时间】:2020-07-30 00:22:11
【问题描述】:

我正在从 Node.js 服务器向 Slack 应用程序发送 JSON 响应。 此响应以原始格式(如 JSON)显示,而不是正确格式化。

复制问题的最少代码:
server.js:

const express = require('express');
const app = express();

// POST request processing
app.post('/', function(req, res){
    var arr1 = [{"type":"section","text":{"type":"mrkdwn","text":"*Lorem Ipsum*"}},
    {"type":"section","text":{"type":"mrkdwn","text":"_Lorem Ipsum_"}},
    {"type":"section","text":{"type":"mrkdwn","text":"`Lorem Ipsum`"}}]
    res.setHeader('Content-Type', 'application/json');
    res.json(arr1);  
});

// Listen to the AppEngine/Heroku - specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log('Server listening on port ${PORT}...');
});

JSON 消息应该在此处呈现为可见: api.slack.com/tools/block-kit-builder

相反,它是这样显示的:

我也尝试过这个和许多其他方法:

res.end(JSON.stringify(arr1));

我在 SO 上找到了这个 proper-way-to-return-json-using-node-or-express,并查看了 Slack 和 Node.js 文档。我的应用程序已经进步了很多,但响应仍然没有正确呈现,所以我想我会在这里问。

有什么想法吗?

【问题讨论】:

标签: node.js json slack


【解决方案1】:

请使用简单的快速回复res.send(your JSON object);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2019-11-14
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多