question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function to Serialize JS objects in Query String Parameters

See original GitHub issue

This is in reference to this line in the code: o.data = Object.keys(o.data).map(function(key){ return key + "=" + encodeURIComponent(o.data[key]); }).join("&");

I just noticed that there is a very helpful utility function nested in ‘fetch’ that is not testable and not reusable even though it has pretty generic functionality (serializing objects into Query String params.)

Thoughts on moving this functionality into it’s own utility method that is testable and reusable?

Lastly, I can make a pull request if necessary but I would suggest adding encodeURIComponent to the ‘key’ variable as well because JavaScript allows characters in object keys that need to be encoded in order to be valid query parameter keys. e.g. var params = { "tom & jerry": "friends" };

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
fridaycommented, Oct 25, 2017

URLSearchParams is useful if you want to manipulate a query string (probably intended for usage pushState and similar), but not as much if you want to build them from scratch from object literals. set only takes strings, so if you pass objects, arrays or other URLSearchParams it will convert them to strings (“[Object object]”). So it won’t work with nested data structures, and for objects that aren’t nested something like this is easier:

Object.entries(data).map(pair => pair.map(encodeURIComponent).join('=')).join('&')

2reactions
Chieftlcommented, Jun 5, 2018

@friday well, I don’t use query-string for that, I use that for query-string 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query-string encoding of a JavaScript object - Stack Overflow
serialize = function(obj) { var str = []; for (var p in obj) if (obj. ... This method converts a JavaScript object into...
Read more >
How to Turn an Object into Query String Parameters in ...
If you know you're keys and values need to be encoded, you should use encodeURIComponent like this: var queryString = Object.keys(params).map ...
Read more >
How to serialize an object into a list of URL query parameters ...
Declare an object and store it into the variable. · Then use JSON. · Use map() method to append the object key value...
Read more >
How to Serialize an Object into a List of URL Query ...
In this article, we'll look at how to serialize an object into a list of URL query parameters with JavaScript. Use the URLSearchParams ......
Read more >
Can'd Goodies: JavaScript Query String Encoding and Decoding
Learn how to serialize JavaScript objects and arrays to URL query strings with can-param, and how to deserialize a query string into an ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found