1

I am trying to send some JSON data from a node js app to another web service written in Flask. I am trying to use the request library and looked at the questions here and here.

Based on the examples I came up with the following simple app. The app sends the JSON data to my flask server, but does not get/fire the callback. I have checked that the Flask application returns some data via postman and it works fine. Where am I going wrong?

[this my entire app.js]

  var express = require("express");
  var request = require('request');
  var app = express();

  app.set('view engine', 'pug');
  app.get('/', function(req, res) {
    console.log("here...")
      request({
          url: 'http://localhost:5000/receive_user_response',
          method: "POST",
          headers: {
              "content-type": "application/json"
          },
          json: {  // <--- I have also tried calling this as "body" 
              user_id: "some id...",
              user_info: "some info..."
          },
        function(err, res, data) {
            console.log('err', err) // <---- never prints any thing from here!
            console.log('res', res)
            console.log('data', data)
              if (!err && res.statusCode == 200) {
                  console.log(data);
              }
          }
    })
    console.log("here...done..")
  });

  var server = app.listen(3000, function() {
      console.log("Listening on port %s...", server.address().port);
  });
Community
  • 1
  • 1
A.D
  • 1,480
  • 2
  • 18
  • 33
  • I'm not sure if this was just a problem putting your code into SO or not, but it appears that you are missing a `}` prior to your callback function. – Matt Altepeter Dec 28 '16 at 23:43
  • are you sure.. looks ok to me... – A.D Dec 28 '16 at 23:47
  • 1
    yeah I'm pretty sure. you have an opening `{` at `request({` followed by properties `url`, `method`, `headers`, and `json`. After the closing `}` for `json`, you immediately have your callback function without closing the `opts` object. `callback` is a separate parameter – Matt Altepeter Dec 28 '16 at 23:53
  • you're right! I thought that request simply takes a list of objects... but in fact it takes 2 objects one is the options and the second is the callback. – A.D Dec 29 '16 at 00:05
  • No worries! I prefer the `request-promise` module myself, anyways! – Matt Altepeter Dec 29 '16 at 00:45
  • if you post an answer i would be happy to accept it.. as you pointed this out before the other answers. – A.D Dec 29 '16 at 02:07

2 Answers2

1

The callback function must be the second parameter of request function. You accidentally place it in the json object. The following would work.

request({
    url: 'http://localhost:5000/receive_user_response',
    method: "POST",
    headers: {
      "content-type": "application/json"
    },
    json: {  // <--- I have also tried calling this as "body" 
      user_id: "some id...",
      user_info: "some info..."
    }
  },
  function(err, res, data) {
    console.log('err', err) // <---- never prints any thing from here!
    console.log('res', res)
    console.log('data', data)
    if (!err && res.statusCode == 200) {
      console.log(data);
    }
  }
)

But this is definitely better to read.

let options = {
  url: 'http://localhost:5000/receive_user_response',
  method: "POST",
  headers: {
    "content-type": "application/json"
  },
  json: {  // <--- I have also tried calling this as "body" 
    user_id: "some id...",
    user_info: "some info..."
  }
};


request(options,
  function(err, res, data) {
    console.log('err', err) // <---- never prints any thing from here!
    console.log('res', res)
    console.log('data', data)
      if (!err && res.statusCode == 200) {
        console.log(data);
      }
  }
)
Nghia Tran
  • 809
  • 1
  • 7
  • 14
0

This is not really the answer but I am able to get it to work by using request-promise instead of request.

var express = require("express");
var app = express();
var rp = require('request-promise');

var options = {
    method: 'POST',
    uri: 'http://localhost:5000/receive_user_response',
    body: {
        some: 'payload'
    },
    json: true // Automatically stringifies the body to JSON
};

app.set('view engine', 'pug');
app.get('/', function(req, res) {

    rp(options).then(function(res) {
        console.log(res);

    }).catch(function(err) {
        console.log("error!");
    });

});

var server = app.listen(3000, function() {
    console.log("Listening on port %s...", server.address().port);
});
A.D
  • 1,480
  • 2
  • 18
  • 33