1

I am sending (rather rying to) send a GET request to my server using the native JavaScript fetch API with an HTML client. On postman I can do this, no problems, what is wrong?

Here is the error:

Unhandled Promise Rejection: TypeError: Request has method 'GET' and cannot have a body

Again, I need to make a GET request, not a post request... Postman does allow this.

Here is the code:

fetch('http://www.localhost:3000/get-data', {
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        a: 1,
        story: document.getElementsByTagName("input")[0].value,
    })
});

PD: I using Safari, bur Chrome, although marking a different looking error that conveys the same message, still does not work.

Xavi Font
  • 296
  • 4
  • 19
  • 4
    it's true - a GET method has no BODY - postman probably converts the "body" to URL query/search params - you should check out the exact request that is made, there will be NO body, guaranteed - if there is, it's more proof that Postman is utter hot garbage – Bravo Jun 09 '22 at 04:20
  • 1
    @WiseEye - how is HTML at all relevant? – Bravo Jun 09 '22 at 04:20
  • @Bravo Heyyy, good to see you again hahaha. What are my options, will using another way to fetch like new XMLHttpRequest()? It does work with GET in postman. – Xavi Font Jun 09 '22 at 04:23
  • 1
    user query params if you prefer HTTP GET. url.com?a=1&story=String or use HTTP Post – jatinder bhola Jun 09 '22 at 04:24
  • 1
    @XaviFont - inspect the actual request from Postman - GET requests MUST NOT have a body - so, sending one is not a valid request and the server should reject the request – Bravo Jun 09 '22 at 04:29
  • @Bravo actually, Postman does let you include a body with GET requests. I'm only aware of the ElasticSearch API where this is even a thing. Browsers on the other hand, do not let you do so – Phil Jun 09 '22 at 04:32
  • @Phil really? so Postman doesn't "convert" body to url search params in a get? – Bravo Jun 09 '22 at 04:33
  • @Bravo nope, it actually sends a body. Some background in [this Github issue](https://github.com/postmanlabs/postman-app-support/issues/131) – Phil Jun 09 '22 at 04:36
  • @Bravo say there were workarounds, I simply should not do it because it is bad practice? Sending a body in a GET request. – Xavi Font Jun 09 '22 at 04:38
  • @XaviFont - as you can see, you can't send a body in a GET request anyway, not in a browser (postman isn't constrained by valid HTTP it seems, and as such deserves the label of hot garbage as it allows invalid requests) – Bravo Jun 09 '22 at 04:40
  • @Bravo it's not invalid though. See the answers in the duplicate. Personally, I also think it's stupid (just like the ElasticSearch API) but it's not going against the 2014 spec – Phil Jun 09 '22 at 04:46
  • @Phil - OK, yeah, just read [the bible](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) and it does say setting a body *may cause some existing implementations to reject the request* :p – Bravo Jun 09 '22 at 05:06

0 Answers0