简体   繁体   English

使用node.js中的请求模块无法执行POST请求

[英]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. 我想将数据发送到在tomcat服务器上运行的java后端。这是到目前为止我尝试过的方法。我已经安装了request module.get方法可以正常工作。

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. 您必须使用JSON.stringify以这种格式发送数据。 before that write console.log(error). 在那之前写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);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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