By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

How to retrieve JSON response data from POST request in nodejs

P: 31
I'm performing some encryption in my server (express routing in node) through a post request:

Expand|Select|Wrap|Line Numbers
  1. app.post('/encrypt', encrypt);
Encrypt is doing:

Expand|Select|Wrap|Line Numbers
  1. function encrypt(req,res) {
  2.         if(req.body.key && req.body.message) {
  3.             var encryptedMessage = Encrypter.encrypt(req.body.key,req.body.message);
  4.  
  5.             return res.status(200).json({ message: encryptedMessage });
  6.          }
  7.  
  8.          res.status(409).json({ message: 'the message could not be encrypted, no key found' });
  9.         }
  10.     }
So, I tested this via console.log, and it's working. When the server receives the request, the encrypted message is being generated.

At the same time, I'm testing my thing with mocha and I'm doing it like so:

Expand|Select|Wrap|Line Numbers
  1.     describe('# Here is where the fun starts ', function () {
  2.       /**
  3.        * Start and stop the server
  4.        */
  5.       before(function () {
  6.         server.listen(port);
  7.       });
  8.  
  9.       after(function () {
  10.         server.close();
  11.       });
  12.  
  13.       it('Requesting an encrypted message', function(done) {
  14.         var postData = querystring.stringify({
  15.           key : key,
  16.           message : message
  17.         });
  18.  
  19.         var options = {
  20.           hostname: hostname,
  21.           port: port,
  22.           path: '/encrypt',
  23.           method: 'POST',
  24.           headers: {
  25.             'Content-Type': 'application/x-www-form-urlencoded',
  26.             'Content-Length': postData.length
  27.           }
  28.         };
  29.  
  30.         var req = http.request(options, function(res) {
  31.           res.statusCode.should.equal(200);
  32.           var encryptedMessage = res.message;
  33.           encryptedMessage.should.not.equal(message);
  34.           done();
  35.         });
  36.  
  37.         req.on('error', function(e) {
  38.           //I'm aware should.fail doesn't work like this
  39.           should.fail('problem with request: ' + e.message);
  40.         });
  41.  
  42.         req.write(postData);
  43.         req.end();
  44.       });
  45.     });
So, whenever I execute the tests, it fails with `Uncaught TypeError: Cannot read property 'should' of undefined` because res.message does not exist.

None of the res.on (data, end, events is working, so I suppose the data should be available from there. First I had this:

Expand|Select|Wrap|Line Numbers
  1.    var req = http.request(options, function(res) {
  2.       res.statusCode.should.equal(200);
  3.       var encryptedMessage;
  4.  
  5.       res.on('data', function (chunk) {
  6.         console.log('BODY: ' + chunk);
  7.         encryptedMessage = chunk.message;
  8.       });
  9.  
  10.       encryptedMessage.should.not.equal(message);
  11.       done();
  12.     });
  13.  
But res.on was never accessed (the console.log didn't show anything). I'm therefore a bit stuck here. I'm surely doing some basic stuff wrong, but I don't have a clue, and the many questions I found doesn't seem to apply to my case.

Weird enough, if I launch a test server and then I curl it
Expand|Select|Wrap|Line Numbers
  1.  curl --data "key=secret&message=veryimportantstuffiabsolutellyneedtoprotect" localhost:2409/encrypt
  2.  
Curl justs waits ad aeternam.
Aug 25 '15 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
it looks like your test setup is insufficient. (additionally, in your code you use variables nowhere defined). since I’m not familiar with testing Node I can only refer you back to the sources where your test setup is explained.
Aug 25 '15 #2

P: 31
Thank you for your kind help

Actually, this is not the complete code, just a simplification. All the variables are defined, otherwise I'd be getting an ECCONREFUSED or similar. As far as I know, the tests are properly configured. I can perform GETS without problems, following the same method, and my POST is sending an answer, but not the data within. You can take a look at the complete code here:

Test (from line 375) -> https://github.com/sieira/sereno/blo.../test/index.js

Encryption (line 43) -> https://github.com/sieira/sereno/blo...no-strategy.js

Test server -> https://github.com/sieira/sereno/blo...test/server.js

Then, I'm still stuck with this...
Aug 26 '15 #3

P: 31
Actually I was doing it properly at the beginning, and the problem was that I was "clearing" my context with done() before the post data arrived. The solution is:

Expand|Select|Wrap|Line Numbers
  1.   var req = http.request(options, function(res) {
  2.     res.statusCode.should.equal(200);
  3.  
  4.     res.on('data', function(data) {
  5.       encryptedMessage = JSON.parse(data).message;
  6.       encryptedMessage.should.not.equal(message);
  7.       done();
  8.     });
  9.   });
In such a way that done() is only called when the data has been threated. Otherwise, mocha will not wait for the answer.
Aug 26 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.