When attempting an Axios request, the backend method cannot be accessed

I am facing an issue when using Axios request to access a method in the backend. I am constrained by pre-existing code in both frontend and backend that limits how much I can modify or add.

Frontend:

  const response = await axiosConfig.put<IReserved>(
    `my_url/${id}`,
    null,
    {
      params: {
        ...body,
      },
    }
  );

Upon checking with console logs, both Id and Body contain correct values. The hurdle seems to be passing them to the backend.

Backend:

    [HttpPut("{id}"), Authorize]
    public async Task<ActionResult<BasicInfoResponse>> UpdateItem(int id, [FromQuery] Request request)

Surprisingly, the code never enters this method, making it impossible to debug.

The final URL submitted (from logs) is as follows:

https://localhost:7216/api/Items/592087867?Var1=900&Var2=592087867&Var3=&Var4=1&Var5=&Var6=&Var7=0882578014

In this URL, Id appears as Var2.

The issue might be that the backend expects Id and other parameters separately, with ID coming first, while here everything is jumbled up. Some variables do not have values, which shouldn't be problematic since they are optional, but could this be causing the problem?

My attempted solutions:

  1. Modifying the structure of the request in the frontend, like adding "Id" to "params" or placing it outside:
      const response = await axiosConfig.put<IReserved>(
        `my_url/${id}`,
        null,
        {
          params: {
            id,
            ...body,
          },
        }
      );
      const response = await axiosConfig.put<IReserved>(
        `my_url/${id}`,
        id,
        {
          params: {
            ...body,
          },
        }
      );
  1. Eliminate "Id" from HttpPut("{id}") in the backend or function parameters.

     [HttpPut, Authorize]
     public async Task<ActionResult<BasicInfoResponse>> UpdateItem(int id, [FromQuery] Request request)
    
    
     [HttpPut("{id}"), Authorize]
     public async Task<ActionResult<BasicInfoResponse>> UpdateItem([FromQuery] Request request)
    
  2. Send all parameters without Id, as it already exists in the URL, or include it as one of the parameters.

      const response = await axiosConfig.put<IReserved>(
        `my_url`,
        null
        {
          params: {
            ...body,
          },
        }
      );
  1. Tried submitting only the Id without any other data, which successfully accesses the method, ruling out possible errors related to wrong function names.

In all scenarios, I receive either status 400 or status 405.

Answer №1

I keep encountering either status 400 or status 405 in all scenarios.

It's impossible for me to debug this, as the code doesn't even enter this specific method.

As specified in the documentation

The [ApiController] attribute causes model validation errors to automatically trigger an HTTP 400 response.

You could try disabling the attribute or following the instructions here to turn off the automatic 400 responses, accessing the controller, and checking the ModelState to pinpoint the issue.

Regarding your 405 error, it seems like it might be related to another endpoint that has a different Http Attribute attached. Share your entire controller so we can attempt to replicate the problem.

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

Leveraging the import statement within lib.d.ts to enhance Intellisense functionality in Visual Studio Code

Looking to streamline my JavaScript project by utilizing custom global variables and harnessing the power of VSCode intellisense for auto completion. Here's what I'm aiming for: See example of auto completion for 'lol' After some sear ...

The ngOnInit child function fails to activate when trying to assign a fresh object

On the parent page, I am passing user data to a child component in this way: <ng-container *ngIf="leaderboard"> <app-leaderboard-preview [user]="user" (click)="goToLeaderboard()"></app-leaderboard-preview> </ng-container> In t ...

Angular is unable to iterate through a collection of ElementRef instances

My list of ElementRef contains all my inputs, but adding listeners to them seems to indicate that textInputs is empty even though it's not. @ViewChildren('text_input') textInputs!: QueryList<ElementRef>; ngAfterViewInit(): void { ...

Retrieve information from the Action controller to communicate with Jquery via an HttpPost or Get request

I am trying to retrieve the result of a calculation in my action controller by calling the action using an AJAX HTTP POST request. I want to capture the value of the calculation and display it in a div element. However, I am unsure whether I should use HTT ...

Experiencing migraines while integrating Firebase 9, Redux Toolkit, and Typescript in a React project. Encountering a peculiar issue where 'dispatch' is unexpectedly identified as type 'never'?

I am currently in the process of upgrading an old project to newer technologies, specifically focusing on Typescript, redux-toolkit, and Firebase v9 for modularity. While I have limited experience with Typescript and none with redux-toolkit, I have been us ...

An issue arises in VueJS when employing brackets and the replace function in Typescript

My journey with the Typescript language has just begun, and I am excited to dive deeper into it. Currently, I am working on a SPA-Wordpress project as a hobby using Vite (VueJS). However, I am facing some challenges with the syntax when transitioning from ...

What are the differences between TypeScript's 'Dictionary' type accessors for objects and objects with predefined members?

Currently, I am delving into TypeScript by following an online tutorial. While my programming background primarily consists of 'structurally' typed languages like C and ActionScript 3, TypeScript presents some new concepts for me to grasp. One p ...

Utilizing Page.Request within the OnLoad event of a Server Control

During the OnLoad event of a server control, I have a specific requirement: string username = Page.Request["UsernameTextBox"]; The controls are actually generated in CreateChildControls (thus only needed on a postback). The issue arises when trying to r ...

"Encountering connectivity issues between NestJs and TypeORM when trying to establish a

My current challenge involves connecting to MySQL. I have set the database connection variables in a .env file located in the root directory, and I am initializing the connection in the app.module.ts file. The problem arises when I attempt to create or run ...

Returns false: CanActivate Observable detects a delay during service validation

Issue with Route Guard in Angular Application: I encountered an issue with my route guard in my Angular application. The problem arises when the guard is active and runs a check by calling a service to retrieve a value. This value is then mapped to true or ...

What is the proper way to reference a computed symbol that has been inherited as a function in an extended class

As a newcomer to Typescript, my understanding of the code might be lacking. I am currently working with the Klasa framework, which is built on top of Discord bot framework using Discord.js. The framework recently introduced plugin functionality and there a ...

What is the definition of a non-arrow React functional component in TypeScript?

Defining types for a React functional component in TypeScript can be done like this: export const Component: React.FC = () => { return // Content }; But how would you define the types for a non-arrow function? function Component() { return // Con ...

Running multiple tests simultaneously using Selenium 4 EdgeDriver in xUnit is not supported

In my project, I have a console application that launches two Edge windows simultaneously and navigates within them successfully: // using statements using OpenQA.Selenium.Edge; // generic driver options List<string> Options = new List<string> ...

Encountered a code 405 error while attempting to make a request to the controller API in an ASP.NET Core React application using Axios

Currently, I am developing a basic application using React and ASP.NET Core where I need to add records to the database utilizing EntityFramework Core. The application consists of a straightforward form with a textbox and a button. However, upon clicking t ...

What is the best way to retrieve the name of a static method within a class?

In my code, I am logging multiple messages in a static method and I want to use the method name as context. However, I do not want to create a separate variable called `context` and assign the function/method name to it. I would like to be able to access ...

"Using TSOA with TypeScript to return an empty array in the response displayed in Postman

I have successfully implemented CRUD operations using TSOA in TypeScript. However, I am facing an issue where I receive an empty array when making HTTP requests, despite adding data to the 'Livraison' table in MongoDB. https://i.sstatic.net/7IWT ...

What is the proper way to utilize variables in package.json with npm version 7.x.x?

I am looking to utilize npm scripts to access keys found in the directories section. "directories": { "client": "client", "server": "server" }, "scripts": { "test:client&qu ...

Axios failing to transmit cookie information, despite setting withCredentials to true

Exploring the capabilities of React and Express to handle requests while including cookies. The communication between client-side and server-side is successful, however, the cookies are not being transmitted. On the client-side: import axios from 'axi ...

Progress Bar Modules

I am currently working on creating a customizable animated progress bar that can be utilized as follows: <bar [type]="'health'" [percentage]="'80'"></bar> It is functional up to the point where I need to adjust different p ...

Unable to locate the type definition file for 'jquery'

After updating my NuGet packages, I encountered an issue where I can no longer compile due to an error related to the bootstrap definition file not being able to find the jquery definition file within my project. Prior to the update, the directory structu ...