【问题标题】:Convert request.js to axios (Vonage api)将 request.js 转换为 axios (Vonage api)
【发布时间】:2021-03-02 05:11:25
【问题描述】:

我正在使用 Vonage api 开发一个信使项目。 此代码运行良好,但我尝试将其转换为 axios 而不是 require.js

var request = require("request");
const data = JSON.stringify({
  from: { type: "messenger", id: process.env.BOT_ID },
  to: { type: "messenger", id: process.env.USER_ID },
  message: {
    content: {
      type: "text",
      text: "Hi There!",
    },
  },
});
var options = {
  url: "https://messages-sandbox.nexmo.com/v0.1/messages",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
  },
  body: data,
  auth: {
    user: process.env.API_KEY,
    pass: process.env.API_SECRET_KEY,
  },
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
}

request(options, callback);

这是具有相同请求的 axios 版本:

axios({
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
  },
  body: {
    from: { type: "messenger", id: process.env.BOT_ID },
    to: { type: "messenger", id: process.env.USER_ID },
    message: {
      content: {
        type: "text",
        text:
          "Hi There!"
      },
    },
  },
  auth: {
    user: process.env.API_KEY,
    pass: process.env.API_SECRET_KEY,
  },
  url: "https://messages-sandbox.nexmo.com/v0.1/messages",
});

而且它不起作用。 我也试过了

  1. axios.post()
  2. 正文的 JSON.stringify

【问题讨论】:

    标签: javascript node.js request axios http-post


    【解决方案1】:

    设置body数据的axios属性是“data”,而不是“body”。这让我以前很困惑。请尝试以下使用控制台日志记录来检查是否发送了正确的请求:

    axios({
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
      },
      data: {
        from: { type: "messenger", id: process.env.BOT_ID },
        to: { type: "messenger", id: process.env.USER_ID },
        message: {
          content: {
            type: "text",
            text:
              "Hi There!"
          },
        },
      },
      auth: {
        user: process.env.API_KEY,
        pass: process.env.API_SECRET_KEY,
      },
      url: "https://messages-sandbox.nexmo.com/v0.1/messages",
    });
    

    【讨论】:

    • 它不起作用我认为问题是关于 CORS 的东西,虽然它与 request.js 配合得很好
    【解决方案2】:

    Vonage 支持向我发送了此代码并且它有效(没有正文或数据)

    axios
      .post(
        "https://messages-sandbox.nexmo.com/v0.1/messages",
        {
          from: { type: "messenger", id: "107083064136738" },
          to: { type: "messenger", id: "3958866477502819" },
          message: {
            content: {
              type: "text",
              text: "You should havee ",
            },
          },
        },
        {
          auth: {
            username: "5aa06d41",
            password: "FMqffmop1GB64X58",
          },
        }
      )
      .then(function (response) {
        console.log("Status: " + response.status);
        console.log(response.data);
      })
      .catch(function (error) {
        console.error(error);
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-17
      • 2021-11-12
      • 2019-06-24
      • 2019-12-18
      • 2020-08-25
      • 2019-07-17
      • 2019-08-17
      • 1970-01-01
      相关资源
      最近更新 更多