Http
JavaScript: Http
Categories:
Create query parameters in JavaScript
// Custom
function encodeQueryData(data) {
const ret = [];
for (let d in data)
ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));
return ret.join('&');
}
let my_query = {
name: 'KJ',
age: 18
};
let searchParams = encodeQueryData(my_query);
// Browser
let my_query = {
name: 'KJ',
age: 18
};
const searchParams = new URLSearchParams(data);
// Node.js
const querystring = require('querystring');
let my_query = {
name: 'KJ',
age: 18
};
const searchParams = querystring.stringify(my_query);
Reference
- url - How to create query parameters in Javascript? - Stack Overflow
- URLSearchParams - Web APIs | MDN