kinotto / geonames.js

Nodejs and Browser client for Geonames.org REST API :earth_africa:
http://www.geonames.org/export/client-libraries.html
MIT License
88 stars 17 forks source link

Cannot enter multiple featureCodes #27

Closed rec-app closed 3 years ago

rec-app commented 3 years ago

I am looking to pass multiple featureCodes as params to the search api end point. I don't seem to be able to do this?

Example:

this.geonames.search({ country: country, q: search, featureCode: 'ADM2', featureCode: 'ADM1' })

kinotto commented 3 years ago

Hi @rec-app geonames.js relies on the axios library to make api calls to geonames Webservices, and in axios you should be able to make api requests with multiple query strings with the same key like in your example, by using an array of strings.

try something like this:

this.geonames.search({ country: country, q: search, featureCode: ['ADM2, 'ADM1'] })

https://stackoverflow.com/questions/42898009/multiple-fields-with-same-key-in-query-params-axios-request

rec-app commented 3 years ago

Great minds think alike!

I did try that unfortunately the resulting request url is featureCode[]=ADM2&featureCode[]=ADM1 which returns all feature codes. Where featureCode=ADM2&featureCode=ADM1 returns the correct results.

Checked out that StackOverflow link and it seems like you can use new URLSearchParams or "you can override params serialization and then use QS NPM module to serialize array with repeat mode"

let myAxios = axios.create({
    paramsSerializer: params => Qs.stringify(params, {arrayFormat: 'repeat'})
})
myAxios.get('path/to/api/',{params}) // URL : https://path/to/api?foo=5&foo=2
kinotto commented 3 years ago

Hey @rec-app i've just published a new release with the change. it should work now, let me know :)

rec-app commented 3 years ago

@kinotto that did the trick, thanks for implementing that so quickly!