Error encountered when attempting to perform a GET request with Next.js version 13.2.3: fetch operation results in TypeError with

I am currently working on a project with a frontend client in Next.js 13.2.3 and a backend in ASP.NET Web API (both saved locally on my pc as separate projects). The backend API is functioning well as I can monitor the requests using Swagger UI, and I can successfully fetch data from https://jsonplaceholder.typicode.com/users in my Next.js app.

The problem arises when I attempt to retrieve data from my backend API within the frontend. I have updated the CORS settings in the backend to allow connections from (the location where the frontend is being served). I am confident that the backend is not the issue since this setup worked without any problems in another React.js project using Vite.

This snippet shows my configuration for CORS in the Program.cs file of my ASP.NET Web API, along with the storage of the "frontend-url" in the appsettings.Development.json file:

(code snippet provided)

In my Next.js project, the code in my page.tsx file located at src\app\dashboard\page.tsx attempts to fetch data:

(code snippet provided)

Upon executing the code, an error is thrown in the browser:

(error details provided)

I am wondering if the issue could be related to hosting both components on the same localhost. I have reviewed the documentation but found no specific information regarding fetching data from localhost servers. Substituting the localhost URL with "https://jsonplaceholder.typicode.com/users" works perfectly. Additionally, I attempted creating a new Next.js project using npm instead of pnpm, but the issue persisted.

Answer â„–1

After @Sebastien Castiel pointed out the necessity of the "https" part, I implemented a try-catch block in the fetch request to uncover the detailed error source. To my surprise, the console log revealed the following:

Error: self signed certificate
  at TLSSocket.onConnectSecure (node:_tls_wrap:1535:34)
  at TLSSocket.emit (node:events:513:28)
  at TLSSocket._finishInit (node:_tls_wrap:949:8)
  at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:730:12)
  at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'}

Curious about SSL certificates in Nextjs development protocols, I discovered that they are not provided for local development. Interestingly, while developing with Vite, this was never an issue as it automatically handles certificates for "https" operations. Your solutions are now clear:

Option 1. You can generate a local certificate by following these steps Calling local dotnet https backend from local NextJS results in FetchError: self-signed certificate. Instructions for MacOS are also available here.

Option 2. In your Visual Studio project solution explorer, locate the properties folder and navigate to the launchSettings.json file to either comment out the "https" url or place the "http" url before it.

{
  ...
  "profiles": {
    ...
      "applicationUrl": "https://localhost:7227;http://localhost:5085",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }  
}

This experience turned out to be quite insightful as I gained a deeper understanding of Secure Sockets Layer (SSL) certificates while working on my Nextjs projects.

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

The standard "Accept" header setting for Asp.Net Web API

When the Accept header is left out of a request to an Asp.Net web API, the server will respond with (415) Unsupported Media Type I am in need of a method to make the API automatically assume a default return type (specifically, application/json) when the ...

Issue with TypeScript Decorators: Setter function is not being invoked for an object's property

I am working on incorporating TypeScript Decorators in an Angular application to store data in local storage. This is my current goal: export function Storage() { return function (target, key): any { // property value let _val = target ...

Accessing data from an object of type Request in NodeJS using Typescript

Is there a way for me to retrieve req.kauth.grant It is definitely populated because when I print req, I see this: kauth: { grant: Grant { access_token: [Token], refresh_token: undefined, id_token: undefined, token_type: undefi ...

pick only one option from each row

I am working on a feature where I have five rows with two checkboxes each generated using a loop and property binding. Currently, clicking on one checkbox selects all elements in the column. However, I want to achieve selection row-wise. Is there a method ...

Error message: "Encountered an issue with Firebase Reacthook: Unable to access properties of undefined (specifically, '_repo

Currently, I am working on developing a hook in Next.js to retrieve user roles from my real-time database. However, I keep encountering an error message stating "Cannot read properties of undefined (reading '_repo')". Below is the implementation ...

Is there a way to convert my messages into different languages without relying on the 'translate' directive or pipe?

Currently, my Angular application is set up with ngx-translate for translation purposes. While it is currently monolingual, I am already looking ahead to the possibility of needing to translate it in the future. Everything is functioning perfectly, but I w ...

Example client for Sanity.io with Next.js (next-sanity)

I am currently in the process of migrating from sanity version 4, which also involves upgrading from nextjs 13.4 to next 14 and next-sanity v7. The main challenge I'm facing is transitioning from version 4 to version 5. To assist with this migration, ...

Deployment failed: Unable to deploy due to the absence of the 'out' directory

Encountering an error when deploying a Next.js app to Netlify can lead to deployment failure. The specific error message "Deploy directory 'out' does not exist" is a common issue that users face. ────────────────┠...

Optimizing your data layer in Angular2: A Guide to Best Practices

As a newcomer to Angular2, I am diving into hands-on learning. My current project involves building multiple views with parent components, child components, and database services. After successfully creating one view, I am now gearing up to implement other ...

Discover the process of retrieving all workday dates using Angular

Currently, I am working on a project in Angular that involves allowing employees to record their work hours. However, I am facing a challenge in figuring out how to gather all the work dates and store them in an array. Here is what I have attempted so fa ...

Activation of Angular SwUpdate deprecation

Within my Angular project, I am currently utilizing the following code snippet: this.swUpdate.available.subscribe(() => { ... }); While this code functions correctly, it does generate a warning regarding the deprecation of activated. How can I addre ...

Why is my Next.js <Image> tag displaying JPG instead of Webp or Avif formats?

When using Nextjs, I anticipated seeing the Image component rendered as .WebP or .AVIF format, but it continues to display jpg instead - https://i.sstatic.net/u1e5P.png Here is my next.config.js file: /** @type {import('next').NextConfig} */ mo ...

Guide to building a straightforward line graph using JSON data from a server

When I retrieve price change data from an API endpoint using axios, the response I receive is in the following format: 0: (2) [1639382650242, 48759.49757943229] 1: (2) [1639386250855, 49131.24712464529] 2: (2) [1639389730718, 48952.65930253478] 3: (2) [163 ...

Modify the innerHTML to adjust font size when a button is clicked in Ionic 5, or eliminate any unnecessary spaces

I have been experimenting with changing the font size of a variable in .html when the variable contains whitespace. In my .ts page, I use the following code to remove the whitespace: this.contents = this.sanitizer.bypassSecurityTrustHtml(this.product[&apos ...

Retrieving a distinct value from an Observable

I am currently attempting to extract the monthlyFee value from this specific response body. ...

Receiving NULL data from client side to server side in Angular 2 + Spring application

I'm currently working on a project that involves using Angular 2 on the client side and Spring on the server side. I need to send user input data from the client to the server and receive a response back. However, I'm encountering an issue where ...

Strategies for effectively searching and filtering nested arrays

I'm facing a challenge with filtering an array of objects based on a nested property and a search term. Here is a sample array: let items = [ { category: 15, label: "Components", value: "a614741f-7d4b-4b33-91b7-89a0ef96a0 ...

How does TypeScript handle the ` import Foo from "Bar" ` statement?

When you bring in a module without using a '.' or '..' prefix For instance: import File from 'FileClass'; How does the ts compiler exactly locate the 'FileClass'? The documentation states Module names can be rel ...

Encountered an issue while resolving symbol values statically within my exclusive set of modules

Encountered an error while resolving symbol values statically. The function 'DataModule' is not supported. Consider using a reference to an exported function instead of a function or lambda, resolving the symbol DataModuleRoot in E:/shopify-clien ...

What is the necessity of requiring a parameter with the type "any"?

There is a function in my code that takes a single parameter of type any: function doSomething(param: any) { // Code to handle the param } When I call this function without passing any arguments: doSomething(); An error is thrown saying: "Expected 1 ...