Happydevdays Happydevdays - 1 month ago 7
Javascript Question

sending key/value data via POST method in a REST API

I'm working on a REST API that includes a POST method to create new widgets.
The data that I need to send looks like this:

key | value
widgetnum | 12345
02:00:00_03:00:00_mtwrfsn | johndoe@myemail.com
12:00:00_15:00:00_mt | janedoe@yahoo.com


Up until now, I have been POSTing data via two keys in the BODY:

widgetnum : 12345
tc : 02:00:00_03:00:00_mtwrfsn_johndoe@myemail.com,12:00:00_15:00:00_mt_janedoe@yahoo.com


As you can see in the example above, the key "tc" actually contains information for two records, just separated by a comma.
But yesterday as i was poking around in the Postman application... I realized that I can send multiple key / value pairs. I don't know why this never clicked before. (such a noob) In any case, I realize now, I can actually do this:

widgetnum : 12345
02:00:00_03:00:00_mtwrfsn : johndoe@myemail.com
12:00:00_15:00:00_mt : janedoe@yahoo.com


Questions

Is it more efficient to change way I POST data as far as my javascript code is concerned? So for example, today, my code looks like this:

router.post('/', function(req, res, next) {
var widgetnum = req.body.widgetnum;
var tc = req.body.tc;
var tc_array = tc.split(",");


aka - I'm able to look for my data using very specific key names.

However, if I change the way I POST my data, I believe I would have to loop to find all the keys.

So something like this:

router.post('/', function(req, res, next) {
//loop through req.body object.
for (var key in req.body) {
if (req.body.hasOwnProperty(key)) {
// add some logic here to test if the key is
// widgetnum or a tc type key.
console.log(key + " -> " + req.body[key]);
}
}
res.status(200).send('ok');
return;
});


I apologize if this seems like a remedial question - it probably is.
But if it's bad form to POST multiple tc records in one key/value pair, I'm happy to change it. Perhaps there's a more efficient way to write my javascript code to find the data.

Thanks.

Answer

To send and receive key/value pair, format already exist that (almost) does the work for you. One of them is JSON, which is widely supported, especially in JavaScript.

Since you are using express already, you can add body-parser and set it to automatically parse JSON:

Data sent (POSTMan: body type: application/json, copy paste in the body.

{
    "widgetnum" : 12345,
    "tc" : {
        "02:00:00_03:00:00_mtwrfsn": johndoe@myemail.com,
        "12:00:00_15:00:00_mt": janedoe@yahoo.com
    } 
}

Server code:

"use strict";

const express = require('express'),
    router = express.Router(),
    bodyParser = require('body-parser');

router.use(bodyParser.json());

router.post('/', function(req, res, next) {
    var widgetnum = req.body.widgetnum;
    var tc = req.body.tc;
    //No more need for tc_array
    //...
}

No more need for your custom format, and req.body will be a JS object of the same form as the JSON sent, so you can do things like:

console.log(req.body.tc['02:00:00_03:00:00_mtwrfsn']); //print johndoe@myemail.com
console.log(Object.keys(req.body.tc)); //Print ['02:00:00_03:00:00_mtwrfsn', '12:00:00_15:00:00_mt']