-1
async function postData(url = '', data = {}) {
    const response = await fetch(url, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            "X-Auth-Token": 'abc'
        },
        body: JSON.stringify(data)
    });
    console.log(response.json());
}

postData('url', { answer: 42 });

Error: Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body. at postData (index.js:3:28) at index.js:14:1

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    What isn't clear about the error message? A GET request can't have a body. Did you mean to use a POST request? – David Aug 26 '22 at 14:06
  • I was able to fetch the data in POSTMAN using GET request – Nagaprasad T S Aug 26 '22 at 14:10
  • All that means is Postman doesn't actively restrict this behavior, whereas it appears that `fetch` does. Bodies in GET requests are non-standard and the behavior is essentially undefined. Different HTTP clients handle it in different ways. You could try something other than `fetch`, or just not use a body on a GET request. – David Aug 26 '22 at 14:13

1 Answers1

0

As the error pointing out you are using the GET method and you could not add a body to it. If you want to store data I would suggest to use POST

You could read more about the http verbs here

MrFabio_25
  • 478
  • 5
  • 9