query-string-encode

Minimal query string encoding, supporting nested objects

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
query-string-encode
101.0.15 years ago6 years agoMinified + gzip package size for query-string-encode in KB

Readme

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.