1

I try use promise-request and write code:

var pr = require('promise-request');

var options = {
  method: 'POST',
  uri: 'http://localhost:4000/user/login',
  port: 4000,
  form: {
    login: 'login',
    password: 'password'
  },
  json: true
};
pr(options)
  .then(function(res) {
    console.log(res);
  })
  .catch(function(err) {
    console.log(err);
  });

If don't use port options I got error:

Error: connect ECONNREFUSED 127.0.0.1:80

And if I add port options error disappear but in official docs don't write about that you can set this option. And it is strage that it works because I set port in uri. I have error that I can not get data from server. I got only error:

undefined:1
<!DOCTYPE html>
^

SyntaxError: Unexpected token < in JSON at position 0

I do same request with http module and all works good maybe I need use another module?

  • Are you sure you have working server on 127.0.0.1:80, to which you addressed your request? To run server on 80 port you must have sudo permission. For second error message you need to remove (for debugging purposes only) `json: true` and see what exact error you are getting. – styopdev May 08 '17 at 09:02
  • I run server on localhost:4000 and don't use port 80 – Metal Evolution Studio May 08 '17 at 09:04

1 Answers1

0

Use axios or request instead of promise-request

Code will look like this:

1.

axios.post('/user/login', {
  login: 'login',
  password: 'password'
})
.then(function(response) {
  console.log(response);
})
.catch(function(error) {
  console.log(error);
});

2.

request.post({
  url: '/user/login',
  form: {
    login: 'login',
    password: 'password'
  }
}, function(err, httpResponse, body) {

})
kaxi1993
  • 4,535
  • 4
  • 29
  • 47