Next.js Discord

Discord Forum

I am fetching a post request but I am getting a 'missing headers' error

Unanswered
European hornet posted this in #help-forum
Open in Discord
European hornetOP
This below is my code:
fetch("https://api.nowpayments.io/v1/auth", requestOptionsToken)
    .then((response) => response.text())
    .then((token) => {
      var myHeaders = new Headers();
      myHeaders.append("Authorization", `Bearer ${JSON.parse(token).token}`);
      myHeaders.append("x-api-key", `${process.env.NPApi}`);
      myHeaders.append("Content-Type", "application/json");

      var raw = JSON.stringify({
        ipn_callback_url: "https://crypto-paypal.vercel.app/api/payment",
        withdrawals: [
          {
            address: data.wallet,
            currency: data.coin,
            amount: 0,
            fiat_amount: data.amount,
            ipn_callback_url: "https://crypto-paypal.vercel.app/api/payment",
          },
        ],
      });

      var requestOptionsPayout = {
        method: "POST",
        headers: myHeaders,
        body: raw,
        redirect: "follow",
      };

      console.log(requestOptionsPayout);

      fetch("https://api.nowpayments.io/v1/payout", requestOptionsPayout)
        .then((response) => response.text())
        .then((result) => console.log(result))
        .catch((error) => console.log("error", error));
    })
    .catch((error) => console.log("Fetching token error: ", error));


First fetch goes through perfectly, but second one is where the issue happens. This is the error I get:
- error TypeError: Cannot read properties of undefined (reading 'headers')
    at /var/task/.next/server/chunks/501.js:5769:61
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)


I logged the token, the headers and it all looks perfectly fine so idk what the issue is.

0 Replies