Get a json via Http Request in NodeJS

后端 未结 3 777
终归单人心
终归单人心 2020-12-07 20:17

Here is my model with a json response:

exports.getUser = function(req, res, callback) {
    User.find(req.body, function (err, data) {
        if (err) {
           


        
3条回答
  •  没有蜡笔的小新
    2020-12-07 20:33

    Just tell request that you are using json:true and forget about header and parse

    var options = {
        hostname: '127.0.0.1',
        port: app.get('port'),
        path: '/users',
        method: 'GET',
        json:true
    }
    request(options, function(error, response, body){
        if(error) console.log(error);
        else console.log(body);
    });
    

    and the same for post

    var options = {
        hostname: '127.0.0.1',
        port: app.get('port'),
        path: '/users',
        method: 'POST',
        json: {"name":"John", "lastname":"Doe"}
    }
    request(options, function(error, response, body){
        if(error) console.log(error);
        else console.log(body);
    });
    

提交回复
热议问题