Sending Request body for GET method in AXIOS throws error

后端 未结 3 764
暖寄归人
暖寄归人 2020-12-20 07:47

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

3条回答
  •  臣服心动
    2020-12-20 08:46

    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.

提交回复
热议问题