在AXIOS中发送GET方法的请求正文会引发错误 [英] Sending Request body for GET method in AXIOS throws error

查看:302
本文介绍了在AXIOS中发送GET方法的请求正文会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个React应用程序,在这里我将POST方法更改为GET,而请求主体保持原样.它可以很好地处理POST请求,但是当我将方法更改为GET时,它会给我错误-

I have a React application where I am changing POST method to GET with the request body as it is. It works fine with POST request however when I change the method to GET, it gives me error-

message: "org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public 

我的前端代码-

export const setData = (getData)  => dispatch => {
    axios({
        method: 'GET',
        url: 'http://localhost:8080/api',
        headers: {
          'Content-Type': 'application/json'
        },
        data: getData
      })
      .then (response => {
      dispatch({
        type: API_DATA, 
        payload: response.data
      })
      dispatch({
        type: SET_SEARCH_LOADER, 
        payload: false
      })
      })
      .catch(function(error) {       
      })
}

有人可以让我知道我在这里想念的东西吗?据我了解,http允许具有GET方法的请求正文.

Can someone let me know what I am missing here. As per my understanding, http allows to have a request body for GET method.

推荐答案

据我了解,http允许具有GET方法的请求正文.

As per my understanding, http allows to have a request body for GET method.

尽管从技术上讲这是正确的(尽管说它只是没有明确禁止它是更准确的),但这是一件很奇怪的事情,并且大多数系统都不希望GET请求具有主体.

While this is technically true (although it may be more accurate to say that it just doesn't explicitly disallow it), it's a very odd thing to do, and most systems do not expect GET requests to have bodies.

因此,许多库将无法处理此问题.

Consequently, plenty of libraries will not handle this.

Axois的文档说:

  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'

在后台,如果您在Web浏览器中运行Axios客户端,它将使用XMLHttpRequest.如果您查看相应的规范,则会显示:

Under the hood, if you run Axios client side in a web browser, it will use XMLHttpRequest. If you look at the specification for that it says:

client . send([body = null])

发起请求. body参数提供了请求正文(如果有的话),并且如果请求方法是GET 或HEAD,则会被忽略.

Initiates the request. The body argument provides the request body, if any, and is ignored if the request method is GET or HEAD.

这篇关于在AXIOS中发送GET方法的请求正文会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆