You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Trading View Webhook触发时请求体为空问题求助

解决TradingView Webhook告警请求体为空的问题

以下是针对你遇到问题的排查和解决步骤:

1. 修正JSON格式的合法性

TradingView要求Webhook的请求体是标准JSON格式,你当前代码里用单引号包裹键和值的写法不符合JSON规范(JSON要求键和字符串值必须用双引号)。

错误写法:

alert("{'heyyy':'oohhhh!'}", freq = alert.freq_all)

正确写法(Pine Script中字符串内部的双引号需用反斜杠转义):

alert("{\"heyyy\":\"oohhhh!\"}", freq = alert.freq_all)

也可以用单引号包裹整个JSON字符串,内部直接使用双引号:

var string payload = '{"heyyy":"oohhhh!"}'
alert(payload, freq = alert.freq_all)

2. 确认TradingView告警的Payload设置

在TradingView创建/编辑告警时,必须确保以下配置正确:

  • 勾选**"Send to webhook URL"**并填写你的Node.js服务器地址
  • 在**"Payload"下拉菜单中选择"Use alert() message"**(这是核心设置,否则TradingView会忽略alert函数传入的自定义消息,发送默认格式或空内容)
  • 再次确认"Alert frequency"设置为**"Every alert() call"**

3. 检查Node.js服务器的请求体解析配置

如果使用Express框架,必须添加express.json()中间件解析JSON请求体,否则req.body会是空对象:

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

// 关键:解析Content-Type为application/json的请求体
app.use(express.json());

app.post('/webhook', (req, res) => {
  console.log('收到的请求体:', req.body);
  res.status(200).send('OK');
});

app.listen(3000, () => {
  console.log('服务器运行在端口3000');
});

如果用原生Node.js,需手动解析请求体:

const http = require('http');

const server = http.createServer((req, res) => {
  if (req.method === 'POST' && req.url === '/webhook') {
    let body = '';
    req.on('data', chunk => body += chunk.toString());
    req.on('end', () => {
      try {
        const payload = JSON.parse(body);
        console.log('解析后的请求体:', payload);
      } catch (err) {
        console.error('JSON解析失败:', err);
      }
      res.writeHead(200);
      res.end('OK');
    });
  } else {
    res.writeHead(404);
    res.end('Not Found');
  }
});

server.listen(3000);

4. 验证alertcondition的正确用法

若使用alertcondition,同样要传入合法JSON字符串,并确保告警设置中选择"Use alert() message":

// 示例:收盘价高于开盘价时触发告警
condition = close > open
alertcondition(condition, title="Bullish Candle", message="{\"signal\":\"bullish\",\"price\":close}")

内容的提问来源于stack exchange,提问作者Maxime Girou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 23:28:20