最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending Request body for GET method in AXIOS throws error - Stack Overflow

programmeradmin5浏览0评论

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 

My Front End Code-

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) {       
      })
}

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

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 

My Front End Code-

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) {       
      })
}

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

Share Improve this question edited Jan 2, 2019 at 12:17 techie_questie asked Jan 2, 2019 at 11:28 techie_questietechie_questie 1,5224 gold badges32 silver badges54 bronze badges 1
  • Axios GET request on Browser uses XMLHttpRequest which doesn't support request body and hence you face this issue. Check this issue on github for more details – Shubham Khatri Commented Jan 2, 2019 at 11:35
Add a comment  | 

3 Answers 3

Reset to default 14

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

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.

The documentation for Axois says:

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

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])

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

If you want to send parameters with get request in axios, you should send parameters as params.

If you want to set "Content-type":"application/json" and send params with get request, you should also send an empty data object.

For example:

const AUTH_TOKEN = 'Bearer token'
const config = {
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': AUTH_TOKEN,
    },
    data: {},
    params: {
        "post_id": 1
    }
}
axios.get("http://localhost/api/v1/posts/", config)

This is not axios, the error origniates from the java backend you're talking to. The public field in your request body is missing.

If you just want to send the data as parameters (which would be odd), pass it using params instead of data (as shown here: https://github.com/axios/axios#example).

I personally don't think your API should support GET with a request body (talk to the devs and ask for documentation).

发布评论

评论列表(0)

  1. 暂无评论