Node.js http 'get' request with query string parameters

I have a Node.js application that is an http client (currently). So I do:

var query = require('querystring').stringify(propertiesObject); http.get(url + query, function(res) { console.log("Got response: " + res.statusCode); }).on('error', function(e) { console.log("Got error: " + e.message); }); 

This seems like a good way to do this. However, I was a little surprised that I should have done the url + query step. This should be encapsulated by a shared library, but I don't see this existing node http library, and I'm not sure which standard npm package could execute it. Is there a more widely used method that is better?

The url.format method saves the work of creating your own url. But ideally, the request will be higher than this.

+43
query-string
Jun 03 '13 at 18:33
source share
3 answers

Open the request module.

It is more fully functional than node's built-in http client.

 var request = require('request'); var propertiesObject = { field1:'test1', field2:'test2' }; request({url:url, qs:propertiesObject}, function(err, response, body) { if(err) { console.log(err); return; } console.log("Get response: " + response.statusCode); }); 
+97
Jun 03 '13 at 19:01
source

If you do not want to use an external package, simply add the following function to your utilities:

 var params=function(req){ let q=req.url.split('?'),result={}; if(q.length>=2){ q[1].split('&').forEach((item)=>{ try { result[item.split('=')[0]]=item.split('=')[1]; } catch (e) { result[item.split('=')[0]]=''; } }) } return result; } 

Then call back in createServer , add the params attribute to the request object:

  http.createServer(function(req,res){ req.params=params(req); // call the function above ; /** * http://mysite/add?name=Ahmed */ console.log(req.params.name) ; // display : "Ahmed" }) 
+3
Jul 19 '16 at 0:59
source

I am trying to add query string parameters to my URL. I couldn't get it to work until I realized what I needed to add ? at the end of my url, otherwise it won't work. This is very important, as it will save you hours of debugging, believe me: I was there ... did it .

The following is a simple API endpoint that calls the Open Weather API and passes APPID , lat and lon as request parameters and return weather data as a JSON object. Hope this helps.

 //Load the request module var request = require('request'); //Load the query String module var querystring = require('querystring'); // Load OpenWeather Credentials var OpenWeatherAppId = require('../config/third-party').openWeather; router.post('/getCurrentWeather', function (req, res) { var urlOpenWeatherCurrent = 'http://api.openweathermap.org/data/2.5/weather?' var queryObject = { APPID: OpenWeatherAppId.appId, lat: req.body.lat, lon: req.body.lon } console.log(queryObject) request({ url:urlOpenWeatherCurrent, qs: queryObject }, function (error, response, body) { if (error) { console.log('error:', error); // Print the error if one occurred } else if(response && body) { console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received res.json({'body': body}); // Print JSON response. } }) }) 

Or, if you want to use the querystring module, make the following changes

 var queryObject = querystring.stringify({ APPID: OpenWeatherAppId.appId, lat: req.body.lat, lon: req.body.lon }); request({ url:urlOpenWeatherCurrent + queryObject }, function (error, response, body) {...}) 
0
Mar 19 '17 at 12:28
source



All Articles