query-string-encode
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

NPM Greenkeeper badge travis size license

query-string-encode

Tiny query string serializer (~20 lines of code). Handles nested objects and arrays. Useful in combination with Fetch and XMLHttpRequest (for example).

Similar to jQuery's $.param and qs.stringify()

Installation

npm i query-string-encode or yarn add query-string-encode

Usage

const queryStringEncode = require('query-string-encode');
queryStringEncode({hello: 'world'})
// Using with Fetch API
fetch('/comment', {
  method: 'post',
  headers: new Headers({'content-type': 'application/x-www-form-urlencoded'}),
  body: queryStringEncode({userId: 1, comment: 'Hello'})
});

Beware

Unlike serialized JSON, query strings are lossy. The only supported data type is string, so it's not possible to distinguishing between "true" vs true or 5 vs "5" or ["a"] vs {0:'a'}. Using JSON is safer. This is an inherent problem with the format and applies to any query string serializer.

Package Sidebar

Install

npm i query-string-encode

Weekly Downloads

32

Version

1.0.1

License

MIT

Unpacked Size

12 kB

Total Files

10

Last publish

Collaborators

  • nullvoidundefined