restify / clients

HttpClient, StringClient, and JsonClient extracted from restify
MIT License
57 stars 34 forks source link

How to use query strings? #134

Closed joh-klein closed 6 years ago

joh-klein commented 7 years ago

looking through the code, I found this: https://github.com/restify/clients/blob/master/lib/HttpClient.js#L722

Evidently doing this is deprecated:

var options = {
  path: '/foo/bar',
  query: {
    foo: 'bar',
    abc: ['xyz', '123']
  }
};

client.get(options, function(err, req, res) { .. });

What is the accepted way then?

DonutEspresso commented 7 years ago

Hey @joh-klein, thanks for pointing this out! TBH, I'm not sure - that comment was before my time. I have been using it the way you have been here in this example, and I don't expect this change anytime soon.

DonutEspresso commented 7 years ago

@yunong maybe you may know?

tanveerrafiqueIVL commented 7 years ago

I also want to know how to use query strings? There's nothing regarding it in docs.

DonutEspresso commented 7 years ago

Hi @tanveerrafiqueIVL, you should continue to use options.query until further notice. We definitely need to get this in the docs, thanks for pointing this out. 👍

tanveerrafiqueIVL commented 7 years ago

So will it work with JSON Client too? Because it wasn't working for me.

joh-klein commented 7 years ago

For the JSONClient I am using "querystring".

So something like this:

var qs = require('querystring');
var params = {
    foo: 'bar',
    abc: ['xyz', '123']
};

var options = {
  path: '/foo/bar?' + qs.stringify(params)
};

client.get(options, function(err, req, res) { .. });
DonutEspresso commented 7 years ago

Apologies @tanveerrafiqueIVL, it looks like I remembered incorrectly. :( I poked through the client code again, it looks like options are getting passed through to underlying http.request method.

@joh-klein's approach is correct for now - you'll have to put it together yourself for the time being, though it appear we could make this easier by adding first class support for it.

tanveerrafiqueIVL commented 7 years ago

Alright. Thank you. :)