Setting query string using Fetch GET request

I'm trying to use the new Fetch API:

I am making a GET request like this:

var request = new Request({
  url: '',
  method: 'GET'

However, I'm unsure how to add a query string to the GET request. Ideally, I want to be able to make a GET request to a URL like:


In jQuery I could do this by passing {order_id: 1} as the data parameter of $.ajax(). Is there an equivalent way to do that with the new Fetch API?



Update March 2017:

URL.searchParams support has officially landed in Chrome 51, but other browsers still require a polyfill.

The official way to work with query parameters is just to add them onto the URL. From the spec, this is an example:

var url = new URL(""),
    params = {lat:35.696233, long:139.570431}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
fetch(url).then(/* … */)

However, I'm not sure Chrome supports the searchParams property of a URL (at the time of writing) so you might want to either use a third party library or roll-your-own solution.

Update April 2018:

With the use of URLSearchParams constructor you could assign a 2D array or a object and just assign that to the instead of looping over all keys and append them

var url = new URL('')

var params = {lat:35.696233, long:139.570431} // or:
var params = [['lat', '35.696233'], ['long', '139.570431']] = new URLSearchParams(params).toString();


Sidenote: URLSearchParams is also available in NodeJS

const { URL, URLSearchParams } = require('url');

As already answered, this is per spec not possible with the fetch-API, yet. But I have to note:

If you are on node, there's the querystring package. It can stringify/parse objects/querystrings:

var querystring = require('querystring')
var data = { key: 'value' }
querystring.stringify(data) // => 'key=value'

...then just append it to the url to request.

However, the problem with the above is, that you always have to prepend a question mark (?). So, another way is to use the parse method from nodes url package and do it as follows:

var url = require('url')
var data = { key: 'value' }
url.format({ query: data }) // => '?key=value'

See query at

This is possible, as it does internally just this:

search = || (
    obj.query && ('?' + (
        typeof(obj.query) === 'object' ?
        querystring.stringify(obj.query) :
) || ''
let params = {
  "param1": "value1",
  "param2": "value2"

let query = Object.keys(params)
             .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))

let url = '' + query;

  .then(data => data.text())
  .then((text) => {
    console.log('request succeeded with JSON response', text)
  }).catch(function (error) {
    console.log('request failed', error)

A concise ES6 approach:

fetch('' + new URLSearchParams({
    foo: 'value',
    bar: 2,

URLSearchParam's toString() function will convert the query args into a string that can be appended onto the URL. In this example, toString() is called implicitly when it gets concatenated with the URL.

IE does not support this feature, but there are polyfills available.


encodeQueryString — encode an object as querystring parameters

 * Encode an object as url query string parameters
 * - includes the leading "?" prefix
 * - example input — {key: "value", alpha: "beta"}
 * - example output — output "?key=value&alpha=beta"
 * - returns empty string when given an empty object
function encodeQueryString(params) {
    const keys = Object.keys(params)
    return keys.length
        ? "?" + keys
            .map(key => encodeURIComponent(key)
                + "=" + encodeURIComponent(params[key]))
        : ""

encodeQueryString({key: "value", alpha: "beta"})
 //> "?key=value&alpha=beta"

Maybe this is better:

const withQuery = require('with-query');

fetch(withQuery('', {
  q: 'query',
  sort: 'stars',
  order: 'asc',
.then(res => res.json())
.then((json) => {;
.catch((err) => {

Template literals are also a valid option here, and provide a few benefits.

You can include raw strings, numbers, boolean values, etc:

    let request = new Request(`${'Patrick'}&number=${1}`);

You can include variables:

    let request = new Request(`${nameParam}`);

You can include logic and functions:

    let request = new Request(`${nameParam !== undefined ? nameParam : getDefaultName() }`);

As far as structuring the data of a larger query string, I like using an array concatenated to a string. I find it easier to understand than some of the other methods:

let queryString = [

let request = new Request(`${queryString}`, {
  method: 'GET'

I know this is stating the absolute obvious, but I feel it's worth adding this as an answer as it's the simplest of all:

var orderId = 1;
var request = new Request({
  url: '' + 1,
  method: 'GET'

Was just working with Nativescript's fetchModule and figured out my own solution using string manipulation. Append the query string bit by bit to the url. Here is an example where query is passed as a json object (query = {order_id: 1}):

function performGetHttpRequest(fetchLink='', query=null) {
    if(query) {
        fetchLink += '?';
        let count = 0;
        const queryLength = Object.keys(query).length;
        for(let key in query) {
            fetchLink += key+'='+query[key];
            fetchLink += (count < queryLength) ? '&' : '';
    // link becomes: ''
    // Then, use fetch as in MDN and simply pass this fetchLink as the url.

I tested this over a multiple number of query parameters and it worked like a charm :) Hope this helps someone.


var paramsdate=01+'%s'+12+'%s'+2012+'%s';



You can use #stringify from query string

import { stringify } from 'query-string';



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.