Skip to content

Instantly share code, notes, and snippets.

@lgk-bsw
Last active September 7, 2020 09:42
Show Gist options
  • Save lgk-bsw/6ad7056dd1ec33a840852b17d2bced67 to your computer and use it in GitHub Desktop.
Save lgk-bsw/6ad7056dd1ec33a840852b17d2bced67 to your computer and use it in GitHub Desktop.
A handy way to convert a JS object to a query string you can pass to an API
const params = {type: "G6.USID", key: "AllgFilter", listPkey: "~ 000G6.USID"};
const query = Object.keys(params)
.map(k => encodeURIComponent(k) + "=" + encodeURIComponent(params[k]))
.join("&")
// Will become: type=G6.USID&key=AllgFilter&listPkey=~%20%20%20000G6.USID
// Usage
fetch(`https://my-api.com/?${query}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment