postman 使用Node.JS实现curl,应用程序只是挂起,没有响应

11dmarpk  于 2022-12-13  发布在  Postman
关注(0)|答案(1)|浏览(181)

所以我尝试传递一个数据,并使用Node从Node.JS端点做一些非常类似于cURL的事情。它挂起,没有响应,什么都没有。我甚至不知道是否有错误。它在Postman中没有响应。不知道是什么想法,因此发表了这篇文章。
我发送给POSTman的数据维斯json

{
  "account_bank":"044",
  "account_number":"0690000040",
  "amount":"700000.25",
  "narration":"test transfer",
  "currency":"NGN",
  "reference":"akhlm-pstmnpyt-rfxx007_PMCKDU_76554",
  "debit_currency":"NGN"
}

和主代码。看起来像这样:

app.post("/api/local-transfers", async function (req, res, next) {
  try {
    if (
      !req.headers.authorization ||
      !req.headers.authorization.startsWith("Bearer ") ||
      !req.headers.authorization.split(" ")[1]
    ) {
      return res.status(422).json({ message: "Please Provide Token!" });
    }

    var account_bank = req.body.account_bank;
    var account_number = req.body.account_number;
    var amount = req.body.amount;
    var narration = req.body.narration;
    var currency = req.body.currency;
    var reference = req.body.refernce;
    var debit_currency = req.body.debit_currency;

    url = "https://api.flutterwave.com/v3/transfers";
    fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-type": "application/json",
        Authorization: "Bearer FLWSECK_TEST-72fe360edef17334f4817a17407011bb-X",
      },
      body: JSON.stringify({
        account_bank: account_bank,
        account_number: account_number,
        amount: amount,
        narration: narration,
        currency: currency,
        reference: reference,
        debit_currency: debit_currency,
      }),
    })
      .then((response) => response.json())
      .then((responseJson) => {
        if (responseJson === "Transfer Queued Successfully") {
          return res.send({
            error: false,
            data: response.json(),
            message: "OK",
          });
        }
      });
  } catch (err) {
    next(err);
  }
});

它只是挂着, Postman 没有回应。会有什么问题?

编辑

请求的.catch如下所示

app.post("/api/local-transfers", async function (req, res, next) {
  try {
    if (
      !req.headers.authorization ||
      !req.headers.authorization.startsWith("Bearer ") ||
      !req.headers.authorization.split(" ")[1]
    ) {
      return res.status(422).json({ message: "Please Provide Token!" });
    }

    var account_bank = req.body.account_bank;
    var account_number = req.body.account_number;
    var amount = req.body.amount;
    var narration = req.body.narration;
    var currency = req.body.currency;
    var reference = req.body.refernce;
    var debit_currency = req.body.debit_currency;

    url = "https://api.flutterwave.com/v3/transfers";
    fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-type": "application/json",
        Authorization: "Bearer FLWSECK_TEST-72fe360edef17334f4817a17407011bb-X",
      },
      body: JSON.stringify({
        account_bank: account_bank,
        account_number: account_number,
        amount: amount,
        narration: narration,
        currency: currency,
        reference: reference,
        debit_currency: debit_currency,
      }),
    })
      .then((response) => response.json())
      .then((responseJson) => {
        if (responseJson === "Transfer Queued Successfully") {
          return res.send({
            error: false,
            data: response.json(),
            message: "OK",
          });
        }
      }).catch(error=>{ //added this catch here.
        throw error;
      });
  } catch (err) {
    next(err);
  }
});
chhkpiq4

chhkpiq41#

好的,我让它工作了。只是碰巧在我这边做了一些读操作。错误是,使用responseJson时,它没有执行该区域内的任何东西,因此更改了它,我设法让它运行。所以将获取从

url = "https://api.flutterwave.com/v3/transfers";
    fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-type": "application/json",
        Authorization: "Bearer FLWSECK_TEST-72fe360edef17334f4817a17407011bb-X",
      },
      body: JSON.stringify({
        account_bank: account_bank,
        account_number: account_number,
        amount: amount,
        narration: narration,
        currency: currency,
        reference: reference,
        debit_currency: debit_currency,
      }),
    })
      .then((response) => response.json())
      .then((responseJson) => {
        if (responseJson === "Transfer Queued Successfully") {
          return res.send({
            error: false,
            data: response.json(),
            message: "OK",
          });
        }
      });
  } catch (err) {
    next(err);
  }
});

对此:

app.post("/api/local-transfers", async function (req, res, next) {
  try {
    if (
      !req.headers.authorization ||
      !req.headers.authorization.startsWith("Bearer ") ||
      !req.headers.authorization.split(" ")[1]
    ) {
      return res.status(422).json({ message: "Please Provide Token!" });
    }

    var account_bank = req.body.account_bank;
    var account_number = req.body.account_number;
    var amount = req.body.amount;
    var narration = req.body.narration;
    var currency = req.body.currency;
    var reference = req.body.refernce;
    var debit_currency = req.body.debit_currency;

    url = "https://api.flutterwave.com/v3/transfers";
    fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-type": "application/json",
        Authorization: "Bearer FLWSECK_TEST-72fe360edef17334f4817a17407011bb-X",
      },
      body: JSON.stringify({
        account_bank: account_bank,
        account_number: account_number,
        amount: amount,
        narration: narration,
        currency: currency,
        reference: reference,
        debit_currency: debit_currency,
      }),
    })
      .then((response) => response.json())
      .then((json) => {
        return res.send({ error: false, data: json, message: "OK" });
      })
      .catch((err) => {
        throw err;
      });
  } catch (err) {
    next(err);
  }
});

相关问题