【问题标题】:POST Requests not working using request module in node.jsPOST 请求无法使用 node.js 中的请求模块
【发布时间】:2017-07-05 06:15:04
【问题描述】:

我想将数据发送到在 tomcat 服务器上运行的 java 后端。这是我迄今为止尝试过的。我已经安装了请求模块。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);
    });

});

【问题讨论】:

  • 那么究竟是什么不起作用?你有错误吗?服务器是否期望除了application/x-www-form-urlencoded 之外的东西?

标签: node.js express request


【解决方案1】:

您必须使用 JSON.stringify 以这种格式发送数据。在那之前写console.log(错误)。并检查您遇到的错误是什么。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多