0

I'm trying to use the request module to send post request to another service but the callback never fires. Here is what I'm doing right now:

request.post(
      `http://localhost:3002/users/login`,
      { 
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({userDetails})
      },
      function (error, response, body) {
          if (!error && response.statusCode == 200) {
              const data = JSON.parse(body);
              console.log(data);
          } else {
              console.log('Request has failed. Please make sure you are logged in');
              res.status(401).send(body);
          }
      }
  );

The callback function never fires. On the other hand, if I try to send this exact request with Postman, the server gets the request. What am I doing wrong?

David Lasry
  • 829
  • 3
  • 12
  • 31
  • does there any kind of errors? – hassan Jul 14 '18 at 12:34
  • @hassan No, I just receive response 500 from this request. The server on port 3002 doesn't receivce the request though so I don't understand where this response code is coming from. – David Lasry Jul 14 '18 at 12:38

1 Answers1

1

Syntax error maybe? Try with:

request({
  method: 'POST',
  url: 'http://localhost:3002/users/login',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({userDetails})
},
function (error, response, body) {
  // Now it should fire the callback.
  console.log('okay');
});

Works like a charm on my side.

Edit: If it still doesn't work, my bet is that the cross-origin resource sharing is not enabled for the localhost:3002 server you're trying to access. Here's the easiest way to enable it.

Kaloyan Kosev
  • 12,483
  • 8
  • 59
  • 90
  • If it works with Postman and it's not a syntax issue, are you sure it's not the "same-origin policy" issue? Is cross-origin resource sharing enabled for the localhost:3002 API you're trying to access? – Kaloyan Kosev Jul 14 '18 at 12:34
  • Yes, it is enabled, I use the `cors` module for that. I never had this problem before. I took the request code from another project and there it works fine. – David Lasry Jul 14 '18 at 12:37
  • Are you importing the correct **request** lib on top of your file (`const request = require('request');`? Which version is it (see in your `package.json`)? I am on 2.87.0 and it works just fine. Try to delete your `node_moduels` folder and do `npm install` again. – Kaloyan Kosev Jul 14 '18 at 12:40
  • Yes, I'm importing the correct request lib, just like you mentioned. I use version 2.87.0 too. I tried to delete `node_modules` folder and installing it again but the problem remains. This is very weird – David Lasry Jul 14 '18 at 12:43
  • Hm. Can you describe the problem in more details? The callback never executes and you don't see the `console.log('okay')`, correct? – Kaloyan Kosev Jul 14 '18 at 12:45
  • Yes. I tried to perform requests to other urls and it works fine. I guess the problem is in my server on port 3002. The weird thing is that if I perform this exact request with Postman, the server gets the request. I have no idea why is that happening – David Lasry Jul 14 '18 at 12:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174991/discussion-between-kaloyan-kosev-and-david-lasry). – Kaloyan Kosev Jul 14 '18 at 12:57
  • Okay, so the problem you are describing is different now. Do you need to wrap the `userDetails` you are sending in an object: `JSON.stringify({userDetails})`? Maybe you don't need to and you should do `JSON.stringify(userDetails)` instead? If this still doesn't solve your issue, let us continue this discussion in the chat. Click the chat link in my previous comment. – Kaloyan Kosev Jul 14 '18 at 13:28
  • Actually I ended up doing something different regarding this problem. I don't know what the problem was unfortunately. – David Lasry Jul 16 '18 at 15:36