在nodeJS中检索POST请求的JSON响应

过度

我正在将POST请求发送到服务,并且应该返回JSON作为响应。我看到POST成功,但没有得到任何回应。我想念什么?下面的代码

var headers = {
    "Accept":"application/json",
    "Content-Type":"application/json",
    "Authorization": ("Basic " + new Buffer("admin:password").toString('base64'))

}




// Configure the request
var options = {
    url: 'myservice-url',
    method : 'POST',
    headers : headers
}


// Start the request
request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        // Print out the response body
        var str = JSON.stringify(body, null, 2);
        console.log(str)

    }
})
马克·瑞安·奥罗萨(Mark Ryan Orosa)

首先,需要确认您是否已正确安装以下组件:

https://github.com/request/request

然后像这样在您的文件中要求它:

var request = require('request');

    request.post({
        url: "",//your url
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        rejectUnauthorized: false,//add when working with https sites
        requestCert: false,//add when working with https sites
        agent: false,//add when working with https sites
        form: {
            whateverfieldnameyouwant: "whatevervalueyouwant"
        }
    },function (response, err, body){
        console.log('Body:',JSON.parse(body));
    }.bind(this));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章