-1

I have an alert in tradingview that send a webhook with a request to my server with a plain body that has some info. the webhook sends PLAIN TEXT, It doecnt sends JSON data.

I was wondering if there is any way to get that plain text data

This is the JSON webhook

WEBHOOK post request

Then this is the code that I have

const express = require('express');

const app = express();

app.post('/webhook', (req, res) => {
    
})

app.listen(8080, () => {console.log("listening on port 8080")})
hoangdv
  • 15,138
  • 4
  • 27
  • 48
LaCK CSGO
  • 1
  • 4

3 Answers3

2

You need to use body parsing middleware.

Your question isn't clear whether the webhook is plain text or JSON. If plain text:

const express = require('express');

const app = express();

app.use(express.text());

app.post('/webhook', (req, res) => {
  console.log(req.body); // If the request has Content-Type text/plain, the body will be parsed as text.
})

app.listen(8080, () => {console.log("listening on port 8080")});

If JSON:

const express = require('express');

const app = express();

app.use(express.json());

app.post('/webhook', (req, res) => {
  console.log(req.body); // If the request has Content-Type application/json, the body will be parsed as JSON.
})

app.listen(8080, () => {console.log("listening on port 8080")});
Aurast
  • 3,189
  • 15
  • 24
0
const express = require('express');
const app = express();
app.use(express.json({limit: '10mb'}));

app.post('/webhook', (req, res) => {
    const { psw, message }  = req.body
})

app.listen(8080, () => {console.log("listening on port 8080")})

this way you can extract body in post request.

Coder_m
  • 51
  • 4
-1

UPDATE: The body-parser middleware is deprecated. The answer provided by @Aurast would be the right way to use the middleware with express.


You need to add a middleware to parse and extract the body of the HTTP request.

You can install the body-parser middleware:

$ npm install body-parser

And then use it as follows:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.text())


Please see this for more information: http://expressjs.com/en/resources/middleware/body-parser.html

Ravi Mashru
  • 497
  • 3
  • 10