node.js - 为什么 Node.js 响应 JSON 在 Slack 中以原始形式显示?
问题描述
我正在从 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 上找到了这个正确的返回 json-using-node-or-express,并查看了 Slack 和 Node.js 文档。我的应用程序已经进步了很多,但响应仍然没有正确呈现,所以我想我会在这里问。
有任何想法吗?
解决方案
请使用简单的快速回复res.send(your JSON object);
推荐阅读
- python - 对除 Python 列表中的第一个值之外的值列表执行计算
- sql - 获取 jsonb 数组中具有相同键的所有对象的列表
- scala - 使用函数文字时scala中的by-name参数问题
- push-notification - 设备恢复在线状态时推送通知
- javascript - 我做了一个汉堡菜单按钮,但它只切换第一个元素
- sql-server - 用于 SQL Server 备份的 Azure 文件共享
- react-native - 反应本机AndroidPermission不授予权限
- java - 导入csv文件master(“yarn”)idea、spark、java
- jasper-reports - 如何在 Jaspersoft studio 6.12.2 中制作多个细节带依次打印数据
- java - Gradle - 为项目中的多个 java 程序运行任务