Why can't the front-end get request pass parameters in the body

1. Problem description

  1. The company's back-end partner wrote such an interface: the request type is the get interface of application/json, and then I adjusted the interface according to the usual way of adjusting the interface, and found that 500 was reported
    insert image description here

  2. I tried it in doc.html and found that 500 was reported
    insert image description here

  3. But the back-end buddy uses postman request to be good
    insert image description here

  4. Later, I found that he put the parameters in the body, so I also put the front-end get request parameters in the body, and continued to adjust the interface and found that the parameters I passed in the data were lost in the browser.

getToBody(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
    
    
    return new Promise((resolve, reject) => {
    
    
      http({
    
    
        url: path,
        data: params,
        headers,
        method: "GET"
      })
        .then(resolve)
        .catch((error) => {
    
    
          if (error !== "-999") {
    
    
            reject(error);
          }
        });
    });
  },

2. Find the reason

The backend interface reported an error, and the main reason for the error was org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing (the required request body was missing ), so I searched for the cause of this problem on the Internet and found that the backend was in The controller has added the @RequestBody annotation, so the request parameters must be placed in the body, but when I use doc.html, he will automatically put the get parameters on the path, and I found out the reason later (in the browser
environment is Axios based on xhr communication ), if the xhr request method is GET or HEAD, the body will be ignored and set to null , so parameters cannot be passed in the body of the GET request in the browser environment . And postman is based on fetch communication, so its get request can pass parameters in the body
insert image description here

3. Solution

Let the backend change the request type to application/x-www-form-urlencoded

Guess you like

Origin blog.csdn.net/qq_42981449/article/details/130840384