Unable to utilize the fetch method in Node.js for sending the API request

I am currently using fetch to send a POST request and retrieve data from an API on the server side (using nextjs 13 + node 18).

Interestingly, I can successfully retrieve the data using postman and axios, but not with fetch.

Below is the snippet of code that I have been working with:

var myHeaders = new Headers();
myHeaders.append("Accept", "application/json");
myHeaders.append("Authorization", "id_token xxx");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
};

fetch("{url}", requestOptions)
  .then(response => response)
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Answer №1

After extensive troubleshooting, I have discovered the root cause of the issue. It appears that the node fetch API includes default headers that are not supported by the API service.

To replicate the same response in postman, I had to include a specific header: Accept-Language: *.

This simple adjustment resolved the issue and allowed me to successfully interact with the API.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue: There were errors encountered while exporting the Next.JS project on the specified path "/", resulting in deployment failure

I am encountering an issue while trying to deploy my nextJS project. The console keeps displaying the following error message: Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error SyntaxError: Unexpect ...

Developing a principal account Twilio client within a subaccount node operation

I am currently working on a Twilio function in a subaccount that needs to connect to a Twilio client in the main account. The function is being deployed via a Github action to the subaccount. To achieve this, I have the following code in my subaccount fun ...

Following a server reboot, an authenticated user will be logged out

I am facing an issue with my Node application where users are getting logged out whenever I update or edit a .js file. The application uses passport-local along with express.io and also incorporates mongoose and socket.io. app.configure(function() { a ...

Utilizing electron as a development dependency in an Ubuntu environment

After installing electron on Ubuntu 17.10, this is the process I followed: ole@mki:~/angular-electron$ npm i --save-dev electron > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9bcb5bcbaadabb6b799e8f7eef7e8eb"> ...

The reverse proxy in nginx is having trouble accessing custom paths within the loopback network

Running my loopback app on the server and attempting to access it via an nginx reverse proxy has been a challenge. Unfortunately, I'm quite new to nginx and struggling to configure it correctly. Below is a snippet from my config file /etc/nginx/sites- ...

I'm struggling to figure out the best method for updating data properties in Vue. Computed properties and watchers aren't getting the job done. What

I'm facing a challenge with updating certain input fields on a form I'm currently working on. After receiving data from axios in one data property, I am attempting to prefill some fields with the response. There are four fields returned from the ...

retrieving information from the database and passing it to the controller

I'm in the process of developing a new API with Express, following the MVC architecture. However, I've been facing difficulties getting the data to successfully return from the database access file to the controllers file. Initially, I attempted ...

Accessing variables from an external script in jsdom

Here is a simple example of jsdom code using the script parameter. Despite my best efforts to reference external JS files, I keep running into this issue: ReferenceError: exVar is not defined Does anyone know what might be causing this problem and how ...

Node's getRandomValues() function is throwing an "expected Uint8Array" error

Currently, I am experimenting with the getRandomValues() function to enhance an encryption REST API that I am developing for practice. My server is using Node, which means I do not have access to a window object containing the crypto object normally housin ...

A more effective solution for saving changes to an existing Model.Document

I have some inquiries regarding the use of .save when exiting a document. I am in search of an effective method for updating a document. I have come across .updateOne(), but I find it unfamiliar as I need to extract the field to be updated and then parse ...

What is the procedure for re-executing the request handler in a Node.js and Express application?

Currently, my setup involves node, express, and mongojs. Here is a code snippet that exemplifies my configuration: function mongoCallback(req, res) { "use strict"; return function (err, o) { if (err) { res.send(500, err.message); } else ...

Having trouble resolving modules after generating tsconfig.json?

I recently added a tsx component to my next.js 13 project following the documentation. After creating the required tsconfig.json file, I encountered module not found errors when running npm run dev: $ npm run dev > [email protected] dev > n ...

Steps for incrementing a number in an integer field with Node.js and MongoDB

I have a dataset that looks like this: { "_id": "6137392141bbb7723", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f7e7fafafef0d5f6f4f2f9f0bbf6faf8">[email protected]</a>", ...

Obtaining the IP addresses of devices connected to my personal Express server within the local network

After developing a basic Web Application with Express, I am able to access the application from devices on my WiFi network while my Express Node server is up and running. How can I identify which specific device has accessed my website? Is it possible fo ...

Guide on adding logout feature with jsonwebtoken in node.js

One common approach is to delete the browser's cookie first. However, I am interested in learning how to destroy tokens from the server side or how to verify logout functionality from the server side. ...

Warning: Outdated version detected during NestJS installation on npm

Whenever I attempt to download NestJS using the command npm i -g @nestjs/cli, I encounter the following issue: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6d5c9d3d4c5c3cb8ed7cbc1ccdcdffccdc3d0"><?[ ...

Error: Docker unable to locate package.json file

I'm encountering an issue. I tried building a project in Docker and received an error. The error states that during npm install, the package.json file could not be found. Here is my Dockerfile: FROM node #copy source COPY . /app # Install dependenc ...

Struggles with the express-validator are present

As a fun project to enhance my backend skills, I decided to create a tic-tac-toe game using Node. However, I encountered an issue while trying to validate the user input for creating a game. The request should only allow the player symbol to be either "x" ...

Server nearby designated to handle requests to the api

Currently, I am working on a project involving automation. Within Adobe CEP, there is a local server that operates on Node.js/Express. My goal is to send an API request from a cloud server to this local server. How can I establish a connection between my l ...

Trouble accessing session cookie during server-side request with getServerSideProps in Next.js

I'm currently facing an issue where I am trying to make a request to a backend service using Axios in getServerSideProps, but it is unsuccessful because the necessary session cookie for authentication is not included in the context.req headers. In th ...