0

I am trying to write a web service in node js. So, basically when someone calls my service through POST and passes the appropriate parameters as JSON then my service should receive the data, call the flutterwave API and get a response and send the response back to the client in JSON. I have been able to do most of it except the sending the response back in JSON format. So, far I am able to print the response in my console but I want to send the response to the client in JSON format. How do I do that ?

app.post('/pay', function (req, res) {
    var jsonString = '';
    var validateoption = req.body.validateoption;
    var authmodel = req.body.authmodel;
    var cardno = req.body.cardno;
    var cvv = req.body.cvv;
    var expirymonth = req.body.expirymonth;
    var expiryyear = req.body.expiryyear;

console.log("Validateoption",req.body.validateoption);
console.log("id",req.body.id);


   /* req.on('end', function () {
        console.log(JSON.parse(jsonString));
    });*/
    flutterwave.Card.tokenize({
        "validateoption":validateoption,
        "authmodel":authmodel, /*Only NOAUTH and BVN are accepted*/
        "bvn": "(Optional:Only needed where authmodel is BVN)",
        "cardno":cardno,
        "cvv":cvv,
        "expirymonth":expirymonth,
        "expiryyear":expiryyear

    }, function(err,body)

    {

    });
});

The response I am getting from Flutterwave is in JSON format and I want to send the JSON response back to my client. This is what I receive

 body:
  { data:
     { responsecode: '00',
       redirecturl: null,
       avsresponsemessage: null,
       avsresponsecode: null,
       responsemessage: 'Completed Successfully',
       otptransactionidentifier: null,
       transactionreference: null,
       responsehtml: null,
       responsetoken: 'YtUyf18D8l06GloAUTH' },
    status: 'success' },
 flutterwaveRequestSuccessful: true,
}
1
  • Just add in function(err,body) => res.json(body); Commented Mar 27, 2017 at 7:24

1 Answer 1

2

You can use res.json():

function(err,body){
   res.json(body.data);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.