Skip to content

querystring => qs, adds support for nested objects - #1301

Merged
hueniverse merged 2 commits into
hapijs:masterfrom
jmonster:qs
Jan 9, 2014
Merged

hueniverse merged 2 commits into
hapijs:masterfrom
jmonster:qs

Conversation

@jmonster

@jmonster jmonster commented Jan 6, 2014

Copy link
Copy Markdown
Contributor

post data

Enables transmitting nested objects with Content-Type application/x-www-form-urlencoded.

curl -XPOST http://localhost:3000/aloha \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'user[name][first]=Johnny&user[email]=jvdomino@gmail.com'

url parameters

Enables nested objects/arrays in URLs; ideal for working with (for example) Ember.JS.

Comments for a post can be loaded by post.get('comments'). The REST adapter will send a GET request to /comments?ids[]=1&ids[]=2&ids[]=3.

input

http://localhost:3000/merchants?ids[]=1&asd=123&user[email]=a@b.c

parsed

before:  { 'ids[]': '1', asd: '123', 'user[email]': 'a@b.c' }
after:  { ids: [ '1' ], asd: '123', user: { email: 'a@b.c' } }

change risk / consequences

  • before: ids[]=1&ids[]=2 => { "ids[]" : [1,2] }
  • after: ids[]=1&ids[]=2 => { "ids" : [1,2] }

Johnny Domino and others added 2 commits January 6, 2014 12:24
Enables transmitting nested objects with Content-Type
`application/x-www-form-urlencoded`
this querystring convention is very popular with emberjs, ruby on
rails, node-express .. and probably others :)
@hueniverse

Copy link
Copy Markdown
Contributor

Are the [] in the query sent with or without %-encoding?

@jmonster

jmonster commented Jan 7, 2014

Copy link
Copy Markdown
Contributor Author

I recall playing with this yesterday and seeing %-encoding show up once (in Chrome) and it worked just the same.
Attaching some screen shots to illustrate behavior from Chrome, including one where I force %5B and %5D.

chrome

screen shot 2014-01-07 at 10 54 27 am
screen shot 2014-01-07 at 10 54 31 am
screen shot 2014-01-07 at 10 54 36 am
screen shot 2014-01-07 at 10 57 02 am
screen shot 2014-01-07 at 10 57 08 am

curl

curl -g http://localhost:3000/aloha\?event\=test\&ids\[\]\=1\&ids\[\]\=2
{
  "code": 400,
  "error": "Bad Request",
  "message": "the key (ids[]) is not allowed",
  "validation": {
    "source": "query",
    "keys": ["ids[]"]
  }
}
curl -g http://localhost:3000/aloha\?event\=test\&ids%5B%5D\=1\&ids%5B%5D\=2
{
  "code": 400,
  "error": "Bad Request",
  "message": "the key (ids[]) is not allowed",
  "validation": {
    "source": "query",
    "keys": ["ids[]"]
  }
}

@hueniverse

Copy link
Copy Markdown
Contributor

Does this prevent you from using ember.js?

@jmonster

jmonster commented Jan 8, 2014

Copy link
Copy Markdown
Contributor Author

it prevents you from using the rest adapter and forces you to roll your own -- but it doesn't outright deny you the ability to use hapi with emberjs. but it makes it a lot harder to get going.

in general, the key[] naming convention is something i learned about it when playing with rails a few years ago: http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters

@hueniverse

Copy link
Copy Markdown
Contributor

This came up once before. I personally dislike it but would like to hear from others before making a decision. It is easy enough to enable with a plugin that overrides the request member in 'onRequest'.

@jmonster

jmonster commented Jan 8, 2014

Copy link
Copy Markdown
Contributor Author

for the sake of understanding -- is it that convention of using [] that you don't like, the dependency on qs rather than node's core querystring module, or something else?

it's unclear to me at this time how I could pass {"some":{"nested":{"object":12345}}} as a querystring without this change. (aside from extending with onRequest -- I mean with vanilla hapi)

@hueniverse

Copy link
Copy Markdown
Contributor

I don't like the [] notation. What's wrong with using an extension? You can just directly change request.query. But I'd like to know what others think.

@jmonster

jmonster commented Jan 8, 2014

Copy link
Copy Markdown
Contributor Author

There's nothing wrong with extending it to serve my purposes, it's your project to take in direction that best suits your team/goals. I personally think it's in hapi's best interest to support these conventions for the sake of wider spread adoption, but given that this is made for walmartlab and you're simply being a hero by sharing it with the OSS community that may be irrelevant -- don't degrade your baby just for me.

It is misleading (to me) though that right now you can submit data via query or payload, and validations exist to check for objects on a querystring, but there is no way to actually send an object via querystring:

~  node
> var q = require('querystring')
undefined
> q.stringify({root:{child:"abracadabra"}})
'root='

@hueniverse

Copy link
Copy Markdown
Contributor

First - this is not for Walmart. This is an open source project used by many other company. I get to make the final decisions but that should never be based on my personal taste. This adds a new dependency and while I certainly appreciate the use case, I want to see if others find it useful to make the complexity trade-off worth it (since you can accomplish the same thing with very little hassle per-need). A few +1s and I'll be happy to find a way to accommodate it (maybe with a config flag).

As for being misleading, the quest does get parsed into an object, even if it is flat...

@ghost ghost assigned hueniverse Jan 9, 2014
@hueniverse
hueniverse merged commit 8b41f84 into hapijs:master Jan 9, 2014
@jmonster

jmonster commented Jan 9, 2014

Copy link
Copy Markdown
Contributor Author

Awesome, thank you sir!

Aside, I didn't mean any insincerity in my hero comment -- I'm genuinely grateful that this project wasn't kept closed source.

jmonster added a commit to jmonster/hapi that referenced this pull request Jan 16, 2014
jmonster added a commit to jmonster/hapi that referenced this pull request Feb 10, 2014
@jmonster
jmonster deleted the qs branch June 29, 2015 20:31
@Marsup Marsup added feature New functionality or improvement and removed request labels Sep 20, 2019
@lock

lock Bot commented Jan 9, 2020

Copy link
Copy Markdown

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock Bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature New functionality or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants