HTTPS request with query strings in NodeJS
21,243
According to https://nodejs.org/api/https.html#https_https_request_options_callback there is no option called "query". You need to include the query parameters to the "path".
Request path. Defaults to '/'. Should include query string if any. E.G. '/index.html?page=12'.
Related videos on Youtube
Author by
mediaashley
Updated on September 26, 2020Comments
-
mediaashley over 3 years
I am trying to make a request to an API using the
get
method in the https module.Looking at HTTPS request in NodeJS, I've got a long way, but my request doesn't appear to include the query parameters…
Using the example from above, with my mods:
var $q = require('q'), _ = require('lodash'), path = require('path'), Qs = require('qs'), path = require('path'), uuid = require('node-uuid'), https = require('https'); var params = { schema: '1.0', form: 'json', username: 'name' } var options = { host: 'openshift.redhat.com', port: 443, path: '/broker/rest/api', query: params }; var req = https.get(options, function(res) {
-
mediaashley almost 8 yearsThat's great! Don't know where I got that
query
option… Apparently I can't accept your answer yet, but I will! Thank you -
n1try almost 8 yearsYou could also use github.com/ljharb/qs to easily serialize an object to a query string and not have to do that string hacking manually.
-
mediaashley almost 8 yearsWe are actually using qs, so from the example above, I have:
+ path: '/broker/rest/api' + Qs.stringify(params)
.