nodejs http.request如何将json参数发送到Java接口

问题描述 投票:0回答:2

我们有一个Java接口可以将verify_code发送到电话,在邮递员那里还不错。

postman

我的nodejs代码如下

let test = {
        "phoneNumber": "15021071273",
        "smsParams": [
            "注册",
            "123456",
            "注册"
        ],
        "tmplId": 109341
    }

    var content = JSON.stringify(test);

    // An object of options to indicate where to post to
    var post_options = {
        host: '172.16.211.33', //'common-message'
        port: '10011',
        path: '/sms/sendTecentyunSms',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': content.length
        }
    };

    // Set up the request
    var post_req = http.request(post_options, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('Response: ' + chunk);
        });
    });

    // post the data
    post_req.write(content);
    post_req.end();

并且Java接口响应:

Response:{“ timestamp”:1572935973619,“ status”:400,“ error”:“ Bad Request”,“ exception”:“ org.springframework.http.converter.HttpMessageNotReadableException”,“ message”:“ JSON解析错误:对象条目内部/之间的意外输入结束;嵌套异常是com.fasterxml.jackson.core.io.JsonEOFException:对象条目内部/之间的意外输入结束\ n在[源:java.io.PushbackInputStream @ 3e497877;行:1,列:157]“,” path“:” / sms / sendTecentyunSms“}

看起来只是一个json解析错误。

我还尝试先对tese.smsParams进行JSON.stringy,但不起作用

req.write仅接受字符串或缓冲区,因此不能仅将json对象作为参数。

javascript java node.js json
2个回答
0
投票

尝试一下:


0
投票

请尝试这种方式

© www.soinside.com 2019 - 2024. All rights reserved.