0

I am pretty new to JavaScript and I'm trying to embed into my code a fetch GET request. I have a GET request that works pretty well if sent with Postman. I translated it with the Code Snippet tool provided by Postman and wrote my function:

function fetchGET() {
    
        var value = "";
    
        var myHeaders = new Headers();
        myHeaders.append("Cookie", "---MyCookie---");
    
        var raw = "";
    
        var requestOptions = {
            method: 'GET',
            headers: myHeaders,
            credentials: 'include',
            body: raw,
            redirect: 'follow'
        };
    
        fetch("---MyURL---", requestOptions)
            .then(response => value = response.text())
            .then(result => value = result)
            .catch(value = "ERROR");
    
        return value;
    }

the only value I get, though, is "ERROR".

If I copy/paste the function into the Chrome console this is the error message I get:

enter image description here

I tried to remove the body but nothing changes. What am I doing wrong? Thank you all!

Paslet87
  • 87
  • 6
  • First do `.catch((err) => console.log(err))` and tell us what the tells you without knowing the actual error we cannot help you. – Palladium02 Aug 25 '22 at 14:57
  • Two problems. You don't seem to understand (yet) how asynchronous JavaScript works (and how to return something from such an asynchronous function -> see the dupe target at the top of your question), and you're not providing a function to `.catch()`, which is what it expects, but instead you're assigning the string `"ERROR"` to `value` and then immediately return that value/string. – Andreas Aug 25 '22 at 14:57
  • @Palladium02 There's no error (maybe). Right now OP just returns the result of the assignment `value = "ERROR"` without ever waiting for the response (or any error caused by that) – Andreas Aug 25 '22 at 14:59
  • @Palladium02 I modified my question by adding the error message I get if i run the script into the Chrome console – Paslet87 Aug 25 '22 at 15:27
  • [Please post text, not images of text.](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question/285557#285557) – Dave Newton Aug 25 '22 at 15:27
  • _"the only value I get, though, is "ERROR"."_ - According to the image you'd added later there's an actual error in the console... And that error tells you what you have to do. – Andreas Aug 25 '22 at 15:50
  • @Andreas I tried to remove the "body" option from the requestoptions but nothing changes.. Now the error message is simply "Failed to fetch" – Paslet87 Aug 25 '22 at 16:10
  • What happened if you change method to POST? Normally you send Body with POST, if you need to send some values. – Choose Goose Aug 25 '22 at 16:21
  • @ChooseGoose i tried but I always get "Failed to fetch" – Paslet87 Aug 25 '22 at 16:26
  • You need to provide response.status here to understand the issue. – Choose Goose Aug 25 '22 at 16:41

0 Answers0