A.D
A.D

Reputation: 1510

node js request.post callback not firing

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);
  });

Upvotes: 1

Views: 6153

Answers (2)

Nghia Tran
Nghia Tran

Reputation: 819

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);
      }
  }
)

Upvotes: 1

A.D
A.D

Reputation: 1510

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);
});

Upvotes: 0

Related Questions