Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body

Tags:

javascript

Getting my API data via Postman works fine but when running following code in my js application I get an error: Request with GET/HEAD method cannot have body.

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer mybearertoken");

var formdata = new FormData();
formdata.append("Id", "56afb645-6084-4129-6469-08d91f4770af");

var requestOptions = {
  method: 'GET',
  headers: myHeaders,
  body: formdata,
  redirect: 'follow'
};

fetch("https://myapi.com/api/Konfigurator/GetProjekt", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
like image 392
daniel Avatar asked Dec 08 '25 14:12

daniel


1 Answers

As per the specification;

If this’s request method is GET or HEAD, then set body to null.

GET and HEAD requests do not have a body, so all parameters should be in the URL.

As for Postman concern, technically, you can send any HTTP request with a body in it as long as the http web server can read it. Nothing prevents get request of having a body. This is an ongoing discussion regarding limitation of how much query string can URL parameters contain and how people need to supply complex parameters into a GET request.

However, some people just tend to keep it as a best practice by utilizing other http methods for body and other went along with it.

like image 61
choz Avatar answered Dec 10 '25 03:12

choz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!