node.js http 'get' request with query string parameters
Solution 1
Check out the request module.
It's more full featured 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);
});
Solution 2
No need for a 3rd party library. Use the nodejs url module to build a URL with query parameters:
const requestUrl = url.parse(url.format({
protocol: 'https',
hostname: 'yoursite.com',
pathname: '/the/path',
query: {
key: value
}
}));
Then make the request with the formatted url. requestUrl.path
will include the query parameters.
const req = https.get({
hostname: requestUrl.hostname,
path: requestUrl.path,
}, (res) => {
// ...
})
Solution 3
If you don't want use external package , Just add the following function in 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 , in createServer
call back , add attribute params
to 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"
})
Solution 4
I have been struggling with how to add query string parameters to my URL. I couldn't make it work until I realized that 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: been there...done that.
Below, is a simple API Endpoint that calls the Open Weather API and passes APPID
, lat
and lon
as query 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) {...})
Solution 5
If you ever need to send GET
request to an IP
as well as a Domain
(Other answers did not mention you can specify a port
variable), you can make use of this function:
function getCode(host, port, path, queryString) {
console.log("(" + host + ":" + port + path + ")" + "Running httpHelper.getCode()")
// Construct url and query string
const requestUrl = url.parse(url.format({
protocol: 'http',
hostname: host,
pathname: path,
port: port,
query: queryString
}));
console.log("(" + host + path + ")" + "Sending GET request")
// Send request
console.log(url.format(requestUrl))
http.get(url.format(requestUrl), (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
console.log("GET chunk: " + chunk);
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log("GET end of response: " + data);
});
}).on("error", (err) => {
console.log("GET Error: " + err);
});
}
Don't miss requiring modules at the top of your file:
http = require("http");
url = require('url')
Also bare in mind that you may use https
module for communicating over secured network.
djechlin
Java, C++, Javascript, Typescript, Angular Background in math / machine learning.
Updated on July 08, 2022Comments
-
djechlin almost 2 years
I have a Node.js application that is an http client (at the moment). So I'm doing:
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 enough way to accomplish this. However I'm somewhat miffed that I had to do the
url + query
step. This should be encapsulated by a common library, but I don't see this existing in node'shttp
library yet and I'm not sure what standard npm package might accomplish it. Is there a reasonably widely used way that's better?url.format method saves the work of building own URL. But ideally the request will be higher level than this also.
-
user264230 over 9 yearsHow would a typical propertiesObject look? I cant get this to work
-
Daniel over 9 yearsThe qs is the query string key. So what ever fields you want in the query string. {field1:'test1',field2:'test2'}
-
Stephen Schaub over 7 yearsThe OP's question concerns http clients, not http servers. This answer is relevant for parsing query strings in an http server, not encoding query strings for an http request.
-
Alexander Mills about 6 yearsAnybody know how to do this with just the Nodejs core http module?
-
peterflynn over 5 yearsThis is doing the opposite of what the question was asking about, and also it's better to use Node's built-in
querystring
module rather than trying to parse this yourself. -
Justin Meiners about 5 years@AlexanderMills see my answer. A 3rd party library is not necessary.
-
Scott Anderson over 4 yearsI am going to try and use this solution because I would like to use some existing code which uses the built-in
https
, however the OP asked for higher-level abstraction and/or libraries for composing URL strings with queries, so I think the accepted answer is more valid personally -
Justin Meiners over 4 years@ScottAnderson I'm ok if i'm not the accepted answer. Just want to help people get done what they need to. Glad it could help you.
-
AmiNadimi over 4 yearsRequest module is now out dated and deprecated.