0

I want to send data to the java back end which is run on tomcat server.this is what i have tried so far.i have already installed request module.get method is working properly.

Router.post('/', function(req, res) {

    request({
        uri: "http://localhost:8080/HIS_API/rest/UserService/registerUser",
        method: "POST",
        form: {
            roleId:2,
            employeeId:26,
            userName:"testing",
            password:"123"
        }
    }, function(error, response, body) {
        console.log(body);
    });

});
robertklep
  • 198,204
  • 35
  • 394
  • 381
Sath.Dev
  • 89
  • 4
  • 18
  • So what exactly isn't working? Do you get errors? Does the server expect something besides `application/x-www-form-urlencoded` perhaps? – robertklep Jul 05 '17 at 06:18

1 Answers1

1

You have to use JSON.stringify to send data like in this format. before that write console.log(error). and check what's the error you are getting.

request({
        url: url, //URL to hit
        method: 'post',
        headers: { "Authorization": req.headers.authorization},//if required
        timeout: 60 * 1000,
        body: JSON.stringify(body)
    }, function (error, result, body) {
        if (error) {
            console.log(error);
        } else if (result.statusCode === 500) {
            console.log('error');
        } else {
            console.log(body);
        }
    });
Shekhar Tyagi
  • 1,644
  • 13
  • 18