Can't seem to get req.body in Express when embedded in Zoho Catalyst framework

Viewed 39

My Zoho Catalyst framework isn't passing the request.body. Here is the code.

module.exports = (req, res1) => {
    const debug = require('debug');
    const https = require('https');
    const tools = require('./tools.js');
    const crypto = require('crypto');

    const express = require('express');
    const app = express();
    app.use(express.json())
    app.use(express.urlencoded({ extended: true }));
    app.use(express.text());


    function getHash(){
    var hmac = crypto.createHmac('sha256', apisecretkey);
    hmac.update(dataToSign);
    return hmac.digest('base64');
    };


    var url = req.url;

    switch (url) {
        case '/scanName':
            //var s = JSON.stringify(req.body)
            console.log(req.body)
            console.log(req.get('Accept'))
            console.log(req.accepts('application/json'));
            res1.write('xx')
            res1.end()
            break;
        case '/':

Here is the output:

undefined
*/*
application/json

I've tried every form of POST from Postman that I can think of, and still nothing.

1 Answers

You have bound the express app variable to recognize the incoming data of the following types JSON, urlencoded and text. But you haven’t used that app variable to get the incoming request so technically it is like you have declared it but never used it. So, your function code couldn’t be able to identify the type of incoming data in the request body. You can modify your code like below:

'use strict';
const debug = require('debug');
const https = require('https');
const tools = require('./tools.js');
const crypto = require('crypto');
const express = require('express');
const app = express();
app.use(express.json());
app.use(express.text());
app.use(express.urlencoded({ extended: true }));   
app.post('/scanName',async(req, res) => {
  try {
    let body = req.body;
    console.log(body);
    res.status(200).send(body);
  } catch (error) {
    console.log(error);
    res.status(500).send(error);
  }        
});
module.exports = app;

You have to export the express app variable to make the endpoints accessible. You can check out this tutorial where we have shown an example of how to get and send data in the Catalyst Advanced IO function using Express.js.

Related