Skip to content

friday/query-string-encode

Repository files navigation

NPM

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.

About

Tiny query string serializer. Handles nested objects and arrays

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •