简体   繁体   中英

POST Requests not working using request module in node.js

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

});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM