提取请求的正文值

时间:2019-07-18 04:11:22

标签: javascript node.js

我想将请求(正文)的值保存在变量中,以便稍后在代码中使用。

我正在用console.log做一些测试。

这有效:

const request = require("request");

var options = {
  url: 'https://host/api/session',
  method: 'POST',
  headers: {
    "Content-Type": "application/json"
  },
  rejectUnauthorized: false,
  json: true,
  body: {
    username: "user",
    password: "password"
  }
};

request(options, function(error, response, body) {

  console.log(body);
}
});

这不起作用。它给出了undefined

const request = require("request");

var options = {
  url: 'https://host/api/session',
  method: 'POST',
  headers: {
    "Content-Type": "application/json"
  },
  rejectUnauthorized: false,
  json: true,
  body: {
    username: "user",
    password: "password"
  }
};


function loginSession() {

  request(options, function(error, response, body) {

      console.log(body);
    }
  });
};

loginSession();

0 个答案:

没有答案