- Encode query string params in a way that Rack-based applications can understand
application/x-www-form-urlencoded
andapplication/json
requests- Nested objects and arrays work 👍
- Universal (Isomorphic)
- (COMING SOON) Capybara tested, use with confidence
Node.js:
$ npm install rack-qs --save
Yarn:
$ yarn add rack-qs
the browser:
<script src="dist/rack-qs.js"></script>
CommonJS:
const rackQs = require('rack-qs');
let obj = { key1: 'value1', key2: [10, 20, 30] };
let str = rackQs.stringify(obj);
// => 'key1=value1&key2%5B%5D=10&key2%5B%5D=20&key2%5B%5D=30'
ES6 import:
import * as rackQs from 'rack-qs';
let obj = { key1: 'value1', key2: [10, 20, 30] };
let str = rackQs.stringify(obj);
// => 'key1=value1&key2%5B%5D=10&key2%5B%5D=20&key2%5B%5D=30'
<script></script>
tag:
let obj = { key1: 'value1', key2: [10, 20, 30] };
let str = window.rackQs.stringify(obj);
// => 'key1=value1&key2%5B%5D=10&key2%5B%5D=20&key2%5B%5D=30'
Rack decodes application/x-www-form-urlencoded
and application/json
requests differently. To specify which kind of request, pass an optional second argument options
.
let obj = { foo: [ { bar: 'baz' } ] };
let options = { form: true };
let str = rackQs.stringify(obj, options);
// => 'foo%5B0%5D%5Bbar%5D=baz'
options.form
is false
by default.
Chrome, Firefox, Safari, Edge, and IE9+.
MIT