0

I have a problem with using Axios request to access method in backend. Unfortunately, I have to use pre-made by others code in both backend and frontend and there is a limit to how much I can change or write anew.

Frontend:

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

I checked with console logs, and both Id and Body seem to have correct values. As I understand, the problem is with passing them on to backend.

Backend:

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

I can't even debug it, the code doesn't enter this method at all.

The final url submitted (from logs) looks like that:

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

Id here appears as Var2.

Might the problem be, that backend wants Id and other params separately and also Id coming first, when here everything is in a "mess"? Also some variables don't have values, indeed I didn't upload any of them, since they should be non-mandatory, but maybe this can cause the problem?

What I tried so far:

  1. Changing the structure of request in frontend, like adding "Id" to "params" or outside of it:
      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. To remove "Id" from HttpPut("{id}") in backend or from 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 only the all params without Id, since it already appears in the url, or/and add it as one of params.

      const response = await axiosConfig.put<IReserved>(
        `my_url`,
        null
        {
          params: {
            ...body,
          },
        }
      );
  1. Tried to submit only the Id without all other stuff, it does access the method, so the error is not with mistyped name of function or whatever.

In all cases I am getting either status 400 or status 405.

2
  • 2
    The only way of debugging is to debug the server code. Otherwise, you are just guessing unless you have good documentation (or samples) of server format. Each server is different and the format of the Request must meet the server requirements. The error is a general purpose error indicating the server did not like either http headers and/or the http body. The connection completed between the client and server and server fund an error while processing the request. If the server connects to a backend the error may be in the connection to backend, but that is just a possibility. Commented Sep 11, 2023 at 14:50
  • Thank you @jdweng, the problem was resolved with Ruikai Feng's answer below. Commented Sep 12, 2023 at 6:01

1 Answer 1

2

In all cases I am getting either status 400 or status 405.

I can't even debug it, the code doesn't enter this method at all.

As mentioned in the document

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

You may try to comment the attribute or follow the guide here to disable the automatic 400 response ,get into the controller and check the ModelState to know where you got wrong

For your 405 error,Seems it was matched with another endpoint that was attached with a different Http Attribute ,show your whole controller so that we could try to reproduce your issue

Sign up to request clarification or add additional context in comments.

1 Comment

Suggestion to disable automatic 400 response helped. The error was causes indeed because some of expected values were null. To fix this, I changed all variable types in relevant model to nullable "public string? Tz". To someone who doesn't know the "?" in model means that the variable can be null. With this I can now enter the backend code in debug mode and process it the usual way. Many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.