0

I'm making a request to an API for some information. I need to send some information in order to get the required information back. The response is in XML format. When I make the request I get the following error

Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

If a GET request can't have a body how do I send the required information? Basically how do I get this to work?

Here is my code.

getResponse = () => {

  const url = 'http://api.pixelz.com/REST.svc/Templates/';

  // The data we are going to send in our request

  let data = JSON.stringify({
    "contactEmail": "[email protected]",
    "contactAPIkey": "MY-API-KEY"
  })

  // The parameters we are gonna pass to the fetch function

  let fetchData = { 
    method: 'GET', 
    body: data,
    headers: new Headers()
  }

  fetch(url, fetchData)
    .then(function(response) {
      // Handle response you get from the server
      response.text()
      .then(data => console.log(data))
  });
}

1 Answer 1

3

Indeed GET requests can't have a body, meaning you don't send data while you are getting. There can be two things going on here.

  1. That specific endpoint is supposed to use another method like POST
  2. The data you want to send actually needs to be passed as querystring parameter http://api.pixelz.com/REST.svc/Templates/?contactAPIkey=....&contactEmail=...
Sign up to request clarification or add additional context in comments.

1 Comment

The http protocol has the concept of verbs, like GET and POST. Each verb has a different reason or meaning. GET is for getting static files, POST is to provide information to the server (generally to create a resource). For that reason, the creators of http decided that sending data on a GET doesn't make logical sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.