Simon Grondin
Simon Grondin

Reputation: 79

How to fix " 'listener' argument must be a function" with nodejs http

I am having trouble correcting this error in my code, which runs fine on local environment, but keeps returning this error : ""listener" argument must be a function" as soon as the code enters my 'http.get' call. Nodejs versions are exactly the same in both environments, and what is troubling is that a get with axios works, with same headers and params. Unfortunately , I cannot use axios because it truncates very important hexadecimal data I am to receive in return of this request.

I have tried writing the function in a more classic style but the same error showed up.

const http = require('https');
const querystring = require('querystring');
const tokenData = require('../config/token');


const apiDL = {
    baseURL: process.env.DOWNLOAD_API,
    headers : {
        //"Content-Type" : "application/x-www-form-urlencoded",
        "Authorization" :""}

    };

 const singleDownload = (req, res) => {
     //truncating my code to arrive to the problematic part
                        let h = apiDL.headers;
                        h.Authorization = token;
                        let p = params;

                            http.get( apiDL.baseURL,{ path :'/download?' + q, headers : h}, (resp) => {

                                const { statusCode } = resp;
                                const contentType = resp.headers['content-type'];
                                const contentDisp = resp.headers['content-disposition'];

//some other code irrelevant to the problem since the bug occurs when the http.get is called
                            }
                            ).on('error', (e) => {
                                console.error(`Got error: ${e.message}`);
                                er +=e.message;
                              }
                            );

 }





 module.exports = {singleDownload}

I do not understand why this error occurs in this environment, while it doesn't locally with the same arguments. I'd appreciate any help.

Upvotes: 1

Views: 1326

Answers (1)

Simon Grondin
Simon Grondin

Reputation: 79

If it helps anyone in the future : the doc specifies that a call like this is possible :

https.get(url,options,callback);

In my case, and I still don't know why, it only accepted this kind of calls :

https.get(options,callback);

Upvotes: 2

Related Questions