xxxxxxxxxx
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// Parse URL-encoded bodies and JSON bodies
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/endpoint', (req, res) => {
// Reading request body
const requestBody = req.body;
// Do something with the body
// For example, send it back as a response
res.send(requestBody);
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
xxxxxxxxxx
var options = {
host: 'www.google.com',
port: 80,
path: '/index.html'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
// File: `pages/api/webhooks/someProvider.ts`
import type { NextApiRequest, NextApiResponse } from 'next';
import type { Readable } from 'node:stream';
// EXPORT config to tell Next.js NOT to parse the body
export const config = {
api: {
bodyParser: false,
},
};
// Get raw body as string
async function getRawBody(readable: Readable): Promise {
const chunks = [];
for await (const chunk of readable) {
chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
}
return Buffer.concat(chunks);
}
// API handler function
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const rawBody = await getRawBody(req);
console.log('raw body for this request is:', rawBody);
const data = JSON.parse(Buffer.from(rawBody).toString('utf8'));
console.log('json data for this request is:', data);
res.send('Got raw body!');
}