Javascript Fetch With GET Query Params (Quick Examples)

Welcome to a tutorial and example on how to send a Javascript fetch request with GET query parameters. New to the fetch API, and trying to pass in some query parameters?

To send GET query parameters with Javascript fetch, simply create a new URL object and append the search parameters:

  • var data = { "KEY" : "VALUE" };
  • var url = new URL("http://site.com/API");
  • for (let k in data) { url.searchParams.append(k, data[k]); }
  • fetch(url);

That covers the quick basics, but read on for the detailed examples.

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Fetch GET Params Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

JAVASCRIPT FETCH WITH GET PARAMS

All right, let us now get into the examples of Javascript fetch with GET parameters.

 

1) APPEND SEARCH PARAMS

1-search-params.html
<script>
function fetchGet () {
  // (A) DATA TO SEND
  var data = {
    name : "jon doe",
    email : "jon@doe.com"
  };
 
  // (B) BUILD URL
  var url = new URL("http://site.com/0-dummy.php");
  for (let k in data) {
    url.searchParams.append(k, data[k]);
  }
  console.log(url);
 
  // (C) FETCH WITH GET SEARCH PARAMS
  fetch(url)
 
  // (D) RETURN SERVER RESPONSE AS TEXT
  .then((result) => {
    console.log(result);
    if (result.status != 200) { throw new Error("Bad Server Response"); }
    return result.text();
  })
  .then((response) => {
    console.log(response);
  })
 
  // (E) HANDLE ERRORS (OPTIONAL)
  .catch((error) => { console.log(error); });
}
</script>
 
<input type="button" value="Fetch Get" onclick="fetchGet()"/>

This is the “full version” of the introduction snippet. As you can see, the parameters are attached to the URL itself, it has nothing much to do with fetch(). Take note of how we generate the URL though:

  • var url = new URL();
  • url.searchParams.append(KEY, VALUE);

The URL search params API is well-supported in all modern browsers at the time of writing, but take note that it will not work on the old Internet Exploders (caniuse.com).

 

 

2) MANUAL URL ENCODE

2-manual-params.html
<script>
function fetchGet () {
  // (A) DATA TO SEND
  var data = {
    name : "jon doe",
    email : "jon@doe.com"
  };
 
  // (B) MANUAL CONSTRUCT URL & PARAMS
  var url = "http://site.com/0-dummy.php";
  url += "?";
  for (let k in data) {
    url += k + "=" + data[k] + "&";
  }
  url = encodeURI(url.slice(0, -1));
 
  // (C) FETCH WITH GET SEARCH PARAMS
  fetch(url)
 
  // (D) RETURN SERVER RESPONSE AS TEXT
  .then((result) => {
    console.log(result);
    if (result.status != 200) { throw new Error("Bad Server Response"); }
    return result.text();
  })
  .then((response) => {
    console.log(response);
  })
 
  // (E) HANDLE ERRORS (OPTIONAL)
  .catch((error) => { console.log(error); });
}
</script>
 
<input type="button" value="Fetch Get" onclick="fetchGet()"/>

If you have to support the older browsers, the only way is to manually construct the URL string and parameters.

  • Simply append the parameters behind the URL – http://site.com/path?KEY=VALUE&KEY=VALUE
  • Then encode the URL using encodeURI().

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Javascript Fetch With GET Params (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *