Node.js set Headers in HTTP GET request

Aneela Saleem Ramzan picture Aneela Saleem Ramzan · Mar 24, 2018 · Viewed 9.4k times · Source

I'm using Node.js 'https' module. And i'm sending a GET request with some headers option but I get the following error:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
    <HEAD>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
        <TITLE>ERROR: The request could not be satisfied</TITLE>
    </HEAD>
    <BODY>
        <H1>ERROR</H1>
        <H2>The request could not be satisfied.</H2>
        <HR noshade size="1px"> Bad request.

        <BR clear="all">
        <HR noshade size="1px">
        <PRE> Generated by cloudfront (CloudFront) Request ID: 7H9MBMS_ullYt-ZSpdMw4WjAU0O1QlVaDNw_8QLwlXZa8olpjmRPDQ== </PRE>
        <ADDRESS></ADDRESS>
    </BODY> </HTML>

Following is my code

 const var https = require('https')
 var optionsget = {
                headers: {
                    'client': process.env.MOVIEGLU_CLIENT,
                    'x-api-key': process.env.MOVIEGLUE_API_KEY,
                    'api-version': process.env.MOVIEGLU_API_VERSION,
                    'Authorization': process.env.MOVIEGLUE_AUTHORISATION,
                    'geolocation': user.loc[0] + ';' + user.loc[1],
                    'Content-Type':'application/x-www-form-urlencoded'
                },
                host: process.env.MOVIEGLU_HOST, 
                path: path,
                method: 'GET' 
            };

            // do the GET request
            var content = ''
            var reqGet = https.request(optionsget, function (response) {
                response.on('data', function (data) {
                    // Append data.
                    content += data;
                });
                response.on('end', function () {

                    console.log('content ', content)
                    callback(null, content)
                })
            });
            reqGet.end();
            reqGet.on('error', function (e) {
                callback(e, null)
            });
        });

Can someone please guide me what i'm doing wrong in setting headers? I'm really stuck.

Thanks

Answer