Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request module node.js: How add query to GET request?

SO I started using the node.js request module to make request to the youtube api. I could make the query string of the below link myself, but I'm pretty sure there is a shortcut. Does anyone know it?

The youtube API link

'https://www.googleapis.com/youtube/v3/search?part=snippet&q=black%20panther&key=AIzaSyD4shfocwn-Ed3Feuoo9fG3d2K2GjHmKeI&maxResults=20&order=viewCount&type=video'

So, I'm looking for a shortcut to add the above query string to my http request

request('https://www.googleapis.com/youtube/v3/search', function (error, response, body) {

});
like image 759
tilly Avatar asked Jun 25 '26 15:06

tilly


1 Answers

You can use querystring in node js just pass a json object with query parameters and it will convert it to query string

const querystring = require('querystring');
const obj = { part: 'snippet', q: 'black' };
const urlQueryString = querystring.stringify(obj);

request('https://www.googleapis.com/youtube/v3/search?' + urlQueryString , 
function (error, response, body) {

});
like image 125
Amr Labib Avatar answered Jun 27 '26 04:06

Amr Labib