1

I'm building a rest api using Express NodeJS with mysql. I'm having no issues at all using GET and using req.params.value and passing values using URL parameters.

Now I'm trying to use POST to insert some data into my database. Using POSTMAN I have no problems doing this because obviously you can set the BODY variables to be used. But it dawned on me in my applications I won't be able to use POSTMAN to do this. My quesiton (which may be silly) is how do I pass these BODY variables to my api? Do I still pass them through the url as parameters? If so, would I use req.body.value or req.params.value? Here is my POST code:

// Add new record 
router.post('/editablerecords/add', function (req, res) {

    let qb_TxnID = req.body.txnid
    let type = req.body.type;
    let margin = req.body.margin;

    if (!qb_TxnID || !type || !margin ) {
        return res.status(400).send({ error:true, message: 'Please provide qb_TxnID, type, or margin' });
    }

    // res.send(qb_TxnID + ' ' + type + ' ' + margin);

    connection.query("INSERT INTO pxeQuoteToClose SET ? ", { 'qb_TxnID': qb_TxnID, 'type': type, 'margin': margin }, function (error, results, fields) {
        if(error){
            res.send(JSON.stringify({"status": 500, "error": error, "response": null})); 
            //If there is error, we send the error in the error section with 500 status
        } else {
            res.send(JSON.stringify({ error: false, data: results, message: 'New record has been created successfully.' }));
            //If there is no error, all is good and response is 200OK.
        }

    });
});
ItsPronounced
  • 5,475
  • 13
  • 47
  • 86
  • you can set a json payload.. not sure the api call off top but a quick google search should get you sorted – mad.meesh Sep 27 '18 at 16:12
  • Parameters passed in a POST request would be in `req.body` – Prerak Sola Sep 27 '18 at 16:16
  • @PrerakSola so would I still need to setup the parameters in my post? For example: `router.post('/editablerecords/add/:txnid&:type', function (req, res) {` – ItsPronounced Sep 27 '18 at 16:28
  • Possible duplicate of [How do you extract POST data in Node.js?](https://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js) – Alexander Kahoun Sep 27 '18 at 19:37
  • @drpcken No. You'll declare `router.post('/editablerecords/add')` and the `req` object in the function will have the data. – Prerak Sola Sep 28 '18 at 08:03

1 Answers1

0

Well it depends on how you will make your request, as an XMLHttpRequest call or via a form, but it will definitely not be using params in that case. Here are three options to send data to your API depending on your requirement, in your case I would advice the last one.

1 - You can use a form and make the action point to to your endpoint. In that case you'll have to add a bodyParser middleware for x-www-form-urlencoded data to your express application. If your using the body-parser library it is as simple as app.use(bodyParser.urlencoded([options])). Data will be available in req.body.

2 - You can send all data in your url but in the query string, not the params. For example: https://yourapi.com/editablerecords/add?qb_TxnID=data_id&type=data_type&margin=data_margin. All data will then be available in the req.query object. No need to add any parser.

3 - Last but not least, I would advise to send your data as a json body using an XMLHttpRequest. To help you out you may use a library like axios or fecth but the principle stay the same: you set your body with your data object and you retrieve it on req.bodyon your api. Seeing your code I do assume that your are using a body parser but if that would not be the case, you should add a middleware using app.use(bodyParser.json()).

I hope I have answer your question and that it will help you out.

P.E. Joëssel
  • 1,393
  • 10
  • 18
  • Thank you for this. I'll look into #3. I am using body parser actually! I will look into XMLHttpRequest. I'm very new to building my own API but it's fascinating. Thank you – ItsPronounced Sep 27 '18 at 21:01