query string code example
Example 1: js query string
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
Example 2: query parameters
Query Parameter
represented as key value pair right after the ?
https://www.google.com/search?q=iloveyou
usually used to filter the result
GET /api/spartacus/search?gender=Male&nameContains=li
if we have more than one query parameter
& is used to connect them
Also there is a Path Parameter|variable
/api/spartans/{id} /api/spartans/:id
It's used to identify single resource amonth list of resources
in above example
the id is the path parameter to identify single spartan
Example 3: string to query string javascript
serialize = function(obj) {
var str = [];
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
console.log(serialize({
foo: "hi there",
bar: "100%"
}));