HTTPS Request (using https nodejs module) of type 'form-data/multipart' not working









up vote
0
down vote

favorite












I have the following curl request which is working nicely and as supposed:




curl --user 'api:MY_API_KEY'
https://api.mailgun.net/v3/mydomain/messages --form from='My Name
' --form to=validaddress@domain.com --form
subject='Hello 3!' --form text='Testing sending email!'




However I need to craft this request as a valid https request using the standard https module form nodejs, and I tried the following code, however I keep getting 400 (bad request) as response:



helpers.sendRequestFormData = function(protocol, port, hostname, method, path, contentType, auth, timeoutSeconds, postData, callback)
var from = 'My Name <info@mydomain>';
var to = 'validaddress@domain.com';
var subject = 'Email test';
var text = 'Testing sending email';

var stringPayload = `--$config.mailgun.boundary
nContent-Disposition: form-data; name="from";
nContent-type: multipart/form-data;
nfrom="$from";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="to";
nContent-type: multipart/form-data;
nto="$to";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="subject";
nContent-type: multipart/form-data;
nsubject="$subject";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="text";
nContent-type: multipart/form-data;
ntext="$text";
n--$config.mailgun.boundaryn`;

// Construct the request
var requestDetails =
'hostname' : hostname,
'port': port,
'method' : method,
'timeout' : timeoutSeconds * 1000,
'path' : path,
'headers' :
'Authorization': auth,
'Content-Type': contentType,
'Content-Length': Buffer.byteLength(stringPayload)

;

// Instantiate the request object (using either the http or https module)
var _moduleToUse = protocol == 'http' ? http : https;
var req = _moduleToUse.request(requestDetails, function(res)

var responseStatus = res.statusCode;
console.log(responseStatus);

res.setEncoding('utf8');
res.on('data', function(data)

if(requestStatus == 200)
callback(false, parsedData);


);
);

// Bind to the error event so it doesn't get thrown
req.on('error',function(e)
console.log(e);
callback(true, 'Error': e);
);

// Bind to the timeout event
req.on('timeout',function()
console.log('timeout');
callback(true, 'Error': 'The request took much time and got timeout.')
);

// Add the payload
req.write(stringPayload);

// End the request
req.end();
;


Can someone please give me some hints, guidance, or tips? I am getting a bit overwhelmed about this, I am sure it might be something simple, been doing trial and error with semi-colons and dashes on the boundary but still didn't got a 200 status response code.



Thank you so much in advance!










share|improve this question





















  • Did your https request set port to 443. I did not see any condition for that
    – front_end_dev
    Nov 11 at 13:24










  • Hi @front_end_dev, yes the port is passed in argument of function
    – TiagoM
    Nov 11 at 13:28










  • If you have a 400 error, that means your request is, in some way, wrong. Try to print out both reaquests and check why they are different.
    – Mario Santini
    Nov 11 at 13:29










  • Hi @MarioSantini that's a good tip yes, I will search out to print out a curl request, maybe -verbose tag or something? I will search thanks!
    – TiagoM
    Nov 11 at 13:32










  • The backtick in Javascript is a way to define a template string, so I think you can get rid of the n characters (and the spaces)...
    – Mario Santini
    Nov 11 at 13:38














up vote
0
down vote

favorite












I have the following curl request which is working nicely and as supposed:




curl --user 'api:MY_API_KEY'
https://api.mailgun.net/v3/mydomain/messages --form from='My Name
' --form to=validaddress@domain.com --form
subject='Hello 3!' --form text='Testing sending email!'




However I need to craft this request as a valid https request using the standard https module form nodejs, and I tried the following code, however I keep getting 400 (bad request) as response:



helpers.sendRequestFormData = function(protocol, port, hostname, method, path, contentType, auth, timeoutSeconds, postData, callback)
var from = 'My Name <info@mydomain>';
var to = 'validaddress@domain.com';
var subject = 'Email test';
var text = 'Testing sending email';

var stringPayload = `--$config.mailgun.boundary
nContent-Disposition: form-data; name="from";
nContent-type: multipart/form-data;
nfrom="$from";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="to";
nContent-type: multipart/form-data;
nto="$to";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="subject";
nContent-type: multipart/form-data;
nsubject="$subject";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="text";
nContent-type: multipart/form-data;
ntext="$text";
n--$config.mailgun.boundaryn`;

// Construct the request
var requestDetails =
'hostname' : hostname,
'port': port,
'method' : method,
'timeout' : timeoutSeconds * 1000,
'path' : path,
'headers' :
'Authorization': auth,
'Content-Type': contentType,
'Content-Length': Buffer.byteLength(stringPayload)

;

// Instantiate the request object (using either the http or https module)
var _moduleToUse = protocol == 'http' ? http : https;
var req = _moduleToUse.request(requestDetails, function(res)

var responseStatus = res.statusCode;
console.log(responseStatus);

res.setEncoding('utf8');
res.on('data', function(data)

if(requestStatus == 200)
callback(false, parsedData);


);
);

// Bind to the error event so it doesn't get thrown
req.on('error',function(e)
console.log(e);
callback(true, 'Error': e);
);

// Bind to the timeout event
req.on('timeout',function()
console.log('timeout');
callback(true, 'Error': 'The request took much time and got timeout.')
);

// Add the payload
req.write(stringPayload);

// End the request
req.end();
;


Can someone please give me some hints, guidance, or tips? I am getting a bit overwhelmed about this, I am sure it might be something simple, been doing trial and error with semi-colons and dashes on the boundary but still didn't got a 200 status response code.



Thank you so much in advance!










share|improve this question





















  • Did your https request set port to 443. I did not see any condition for that
    – front_end_dev
    Nov 11 at 13:24










  • Hi @front_end_dev, yes the port is passed in argument of function
    – TiagoM
    Nov 11 at 13:28










  • If you have a 400 error, that means your request is, in some way, wrong. Try to print out both reaquests and check why they are different.
    – Mario Santini
    Nov 11 at 13:29










  • Hi @MarioSantini that's a good tip yes, I will search out to print out a curl request, maybe -verbose tag or something? I will search thanks!
    – TiagoM
    Nov 11 at 13:32










  • The backtick in Javascript is a way to define a template string, so I think you can get rid of the n characters (and the spaces)...
    – Mario Santini
    Nov 11 at 13:38












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the following curl request which is working nicely and as supposed:




curl --user 'api:MY_API_KEY'
https://api.mailgun.net/v3/mydomain/messages --form from='My Name
' --form to=validaddress@domain.com --form
subject='Hello 3!' --form text='Testing sending email!'




However I need to craft this request as a valid https request using the standard https module form nodejs, and I tried the following code, however I keep getting 400 (bad request) as response:



helpers.sendRequestFormData = function(protocol, port, hostname, method, path, contentType, auth, timeoutSeconds, postData, callback)
var from = 'My Name <info@mydomain>';
var to = 'validaddress@domain.com';
var subject = 'Email test';
var text = 'Testing sending email';

var stringPayload = `--$config.mailgun.boundary
nContent-Disposition: form-data; name="from";
nContent-type: multipart/form-data;
nfrom="$from";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="to";
nContent-type: multipart/form-data;
nto="$to";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="subject";
nContent-type: multipart/form-data;
nsubject="$subject";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="text";
nContent-type: multipart/form-data;
ntext="$text";
n--$config.mailgun.boundaryn`;

// Construct the request
var requestDetails =
'hostname' : hostname,
'port': port,
'method' : method,
'timeout' : timeoutSeconds * 1000,
'path' : path,
'headers' :
'Authorization': auth,
'Content-Type': contentType,
'Content-Length': Buffer.byteLength(stringPayload)

;

// Instantiate the request object (using either the http or https module)
var _moduleToUse = protocol == 'http' ? http : https;
var req = _moduleToUse.request(requestDetails, function(res)

var responseStatus = res.statusCode;
console.log(responseStatus);

res.setEncoding('utf8');
res.on('data', function(data)

if(requestStatus == 200)
callback(false, parsedData);


);
);

// Bind to the error event so it doesn't get thrown
req.on('error',function(e)
console.log(e);
callback(true, 'Error': e);
);

// Bind to the timeout event
req.on('timeout',function()
console.log('timeout');
callback(true, 'Error': 'The request took much time and got timeout.')
);

// Add the payload
req.write(stringPayload);

// End the request
req.end();
;


Can someone please give me some hints, guidance, or tips? I am getting a bit overwhelmed about this, I am sure it might be something simple, been doing trial and error with semi-colons and dashes on the boundary but still didn't got a 200 status response code.



Thank you so much in advance!










share|improve this question













I have the following curl request which is working nicely and as supposed:




curl --user 'api:MY_API_KEY'
https://api.mailgun.net/v3/mydomain/messages --form from='My Name
' --form to=validaddress@domain.com --form
subject='Hello 3!' --form text='Testing sending email!'




However I need to craft this request as a valid https request using the standard https module form nodejs, and I tried the following code, however I keep getting 400 (bad request) as response:



helpers.sendRequestFormData = function(protocol, port, hostname, method, path, contentType, auth, timeoutSeconds, postData, callback)
var from = 'My Name <info@mydomain>';
var to = 'validaddress@domain.com';
var subject = 'Email test';
var text = 'Testing sending email';

var stringPayload = `--$config.mailgun.boundary
nContent-Disposition: form-data; name="from";
nContent-type: multipart/form-data;
nfrom="$from";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="to";
nContent-type: multipart/form-data;
nto="$to";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="subject";
nContent-type: multipart/form-data;
nsubject="$subject";
n--$config.mailgun.boundary
nContent-Disposition: form-data; name="text";
nContent-type: multipart/form-data;
ntext="$text";
n--$config.mailgun.boundaryn`;

// Construct the request
var requestDetails =
'hostname' : hostname,
'port': port,
'method' : method,
'timeout' : timeoutSeconds * 1000,
'path' : path,
'headers' :
'Authorization': auth,
'Content-Type': contentType,
'Content-Length': Buffer.byteLength(stringPayload)

;

// Instantiate the request object (using either the http or https module)
var _moduleToUse = protocol == 'http' ? http : https;
var req = _moduleToUse.request(requestDetails, function(res)

var responseStatus = res.statusCode;
console.log(responseStatus);

res.setEncoding('utf8');
res.on('data', function(data)

if(requestStatus == 200)
callback(false, parsedData);


);
);

// Bind to the error event so it doesn't get thrown
req.on('error',function(e)
console.log(e);
callback(true, 'Error': e);
);

// Bind to the timeout event
req.on('timeout',function()
console.log('timeout');
callback(true, 'Error': 'The request took much time and got timeout.')
);

// Add the payload
req.write(stringPayload);

// End the request
req.end();
;


Can someone please give me some hints, guidance, or tips? I am getting a bit overwhelmed about this, I am sure it might be something simple, been doing trial and error with semi-colons and dashes on the boundary but still didn't got a 200 status response code.



Thank you so much in advance!







javascript node.js https multipartform-data






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 13:10









TiagoM

1,45042560




1,45042560











  • Did your https request set port to 443. I did not see any condition for that
    – front_end_dev
    Nov 11 at 13:24










  • Hi @front_end_dev, yes the port is passed in argument of function
    – TiagoM
    Nov 11 at 13:28










  • If you have a 400 error, that means your request is, in some way, wrong. Try to print out both reaquests and check why they are different.
    – Mario Santini
    Nov 11 at 13:29










  • Hi @MarioSantini that's a good tip yes, I will search out to print out a curl request, maybe -verbose tag or something? I will search thanks!
    – TiagoM
    Nov 11 at 13:32










  • The backtick in Javascript is a way to define a template string, so I think you can get rid of the n characters (and the spaces)...
    – Mario Santini
    Nov 11 at 13:38
















  • Did your https request set port to 443. I did not see any condition for that
    – front_end_dev
    Nov 11 at 13:24










  • Hi @front_end_dev, yes the port is passed in argument of function
    – TiagoM
    Nov 11 at 13:28










  • If you have a 400 error, that means your request is, in some way, wrong. Try to print out both reaquests and check why they are different.
    – Mario Santini
    Nov 11 at 13:29










  • Hi @MarioSantini that's a good tip yes, I will search out to print out a curl request, maybe -verbose tag or something? I will search thanks!
    – TiagoM
    Nov 11 at 13:32










  • The backtick in Javascript is a way to define a template string, so I think you can get rid of the n characters (and the spaces)...
    – Mario Santini
    Nov 11 at 13:38















Did your https request set port to 443. I did not see any condition for that
– front_end_dev
Nov 11 at 13:24




Did your https request set port to 443. I did not see any condition for that
– front_end_dev
Nov 11 at 13:24












Hi @front_end_dev, yes the port is passed in argument of function
– TiagoM
Nov 11 at 13:28




Hi @front_end_dev, yes the port is passed in argument of function
– TiagoM
Nov 11 at 13:28












If you have a 400 error, that means your request is, in some way, wrong. Try to print out both reaquests and check why they are different.
– Mario Santini
Nov 11 at 13:29




If you have a 400 error, that means your request is, in some way, wrong. Try to print out both reaquests and check why they are different.
– Mario Santini
Nov 11 at 13:29












Hi @MarioSantini that's a good tip yes, I will search out to print out a curl request, maybe -verbose tag or something? I will search thanks!
– TiagoM
Nov 11 at 13:32




Hi @MarioSantini that's a good tip yes, I will search out to print out a curl request, maybe -verbose tag or something? I will search thanks!
– TiagoM
Nov 11 at 13:32












The backtick in Javascript is a way to define a template string, so I think you can get rid of the n characters (and the spaces)...
– Mario Santini
Nov 11 at 13:38




The backtick in Javascript is a way to define a template string, so I think you can get rid of the n characters (and the spaces)...
– Mario Santini
Nov 11 at 13:38












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I made it working, the current code is the following:



helpers.sendRequest = function(protocol, port, hostname, method, path, 

contentType, auth, timeoutSeconds, postData, callback)
var stringPayload = querystring.stringify(postData);

// Construct the request
var requestDetails =
'hostname' : hostname,
'port': port,
'method' : method,
'timeout' : timeoutSeconds * 1000,
'path' : path
;

// Instantiate the request object (using either the http or https module)
var _moduleToUse = protocol == 'http' ? http : https;
var req = _moduleToUse.request(requestDetails, function(res)
res.on('data', (d) =>
if(res.statusCode == 200)
callback(false);
else
console.log(res.statusCode);
callback(true);

);
);

req.setHeader('Authorization', auth);
req.setHeader('Content-Type', contentType);
req.setHeader('Content-Length', Buffer.byteLength(stringPayload));

// Add the payload
req.write(stringPayload);

// Bind to the error event so it doesn't get thrown
req.on('error',function(e)
console.log(e);
callback(true, 'Error': e);
);

// Bind to the timeout event
req.on('timeout',function()
console.log('timeout');
callback(true, 'Error': 'The request took much time and got timeout.')
);

// End the request
req.end();
;


And I call the method like this:



genericHelper.sendRequest('https', '443', 'api.mailgun.net', 'POST', '/v3/sandbox0630029a67f24517a9c3e383d2c6098e.mailgun.org/messages',
'application/x-www-form-urlencoded', ('Basic ' + Buffer.from(('api:'+ config.mailgun.ApiKeyTest)).toString('base64')), 5, emailRequestObject, function(err, data)

// Here I do what I need after sending the http request with success

);


I hope it helps someone out, so the problem was the content-type, I had to change it to 'application/x-www-form-urlencoded' and then also the authorization I had to convert to Base64 and include Basic before the pair in base64.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249061%2fhttps-request-using-https-nodejs-module-of-type-form-data-multipart-not-work%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I made it working, the current code is the following:



    helpers.sendRequest = function(protocol, port, hostname, method, path, 

    contentType, auth, timeoutSeconds, postData, callback)
    var stringPayload = querystring.stringify(postData);

    // Construct the request
    var requestDetails =
    'hostname' : hostname,
    'port': port,
    'method' : method,
    'timeout' : timeoutSeconds * 1000,
    'path' : path
    ;

    // Instantiate the request object (using either the http or https module)
    var _moduleToUse = protocol == 'http' ? http : https;
    var req = _moduleToUse.request(requestDetails, function(res)
    res.on('data', (d) =>
    if(res.statusCode == 200)
    callback(false);
    else
    console.log(res.statusCode);
    callback(true);

    );
    );

    req.setHeader('Authorization', auth);
    req.setHeader('Content-Type', contentType);
    req.setHeader('Content-Length', Buffer.byteLength(stringPayload));

    // Add the payload
    req.write(stringPayload);

    // Bind to the error event so it doesn't get thrown
    req.on('error',function(e)
    console.log(e);
    callback(true, 'Error': e);
    );

    // Bind to the timeout event
    req.on('timeout',function()
    console.log('timeout');
    callback(true, 'Error': 'The request took much time and got timeout.')
    );

    // End the request
    req.end();
    ;


    And I call the method like this:



    genericHelper.sendRequest('https', '443', 'api.mailgun.net', 'POST', '/v3/sandbox0630029a67f24517a9c3e383d2c6098e.mailgun.org/messages',
    'application/x-www-form-urlencoded', ('Basic ' + Buffer.from(('api:'+ config.mailgun.ApiKeyTest)).toString('base64')), 5, emailRequestObject, function(err, data)

    // Here I do what I need after sending the http request with success

    );


    I hope it helps someone out, so the problem was the content-type, I had to change it to 'application/x-www-form-urlencoded' and then also the authorization I had to convert to Base64 and include Basic before the pair in base64.






    share|improve this answer
























      up vote
      0
      down vote













      I made it working, the current code is the following:



      helpers.sendRequest = function(protocol, port, hostname, method, path, 

      contentType, auth, timeoutSeconds, postData, callback)
      var stringPayload = querystring.stringify(postData);

      // Construct the request
      var requestDetails =
      'hostname' : hostname,
      'port': port,
      'method' : method,
      'timeout' : timeoutSeconds * 1000,
      'path' : path
      ;

      // Instantiate the request object (using either the http or https module)
      var _moduleToUse = protocol == 'http' ? http : https;
      var req = _moduleToUse.request(requestDetails, function(res)
      res.on('data', (d) =>
      if(res.statusCode == 200)
      callback(false);
      else
      console.log(res.statusCode);
      callback(true);

      );
      );

      req.setHeader('Authorization', auth);
      req.setHeader('Content-Type', contentType);
      req.setHeader('Content-Length', Buffer.byteLength(stringPayload));

      // Add the payload
      req.write(stringPayload);

      // Bind to the error event so it doesn't get thrown
      req.on('error',function(e)
      console.log(e);
      callback(true, 'Error': e);
      );

      // Bind to the timeout event
      req.on('timeout',function()
      console.log('timeout');
      callback(true, 'Error': 'The request took much time and got timeout.')
      );

      // End the request
      req.end();
      ;


      And I call the method like this:



      genericHelper.sendRequest('https', '443', 'api.mailgun.net', 'POST', '/v3/sandbox0630029a67f24517a9c3e383d2c6098e.mailgun.org/messages',
      'application/x-www-form-urlencoded', ('Basic ' + Buffer.from(('api:'+ config.mailgun.ApiKeyTest)).toString('base64')), 5, emailRequestObject, function(err, data)

      // Here I do what I need after sending the http request with success

      );


      I hope it helps someone out, so the problem was the content-type, I had to change it to 'application/x-www-form-urlencoded' and then also the authorization I had to convert to Base64 and include Basic before the pair in base64.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        I made it working, the current code is the following:



        helpers.sendRequest = function(protocol, port, hostname, method, path, 

        contentType, auth, timeoutSeconds, postData, callback)
        var stringPayload = querystring.stringify(postData);

        // Construct the request
        var requestDetails =
        'hostname' : hostname,
        'port': port,
        'method' : method,
        'timeout' : timeoutSeconds * 1000,
        'path' : path
        ;

        // Instantiate the request object (using either the http or https module)
        var _moduleToUse = protocol == 'http' ? http : https;
        var req = _moduleToUse.request(requestDetails, function(res)
        res.on('data', (d) =>
        if(res.statusCode == 200)
        callback(false);
        else
        console.log(res.statusCode);
        callback(true);

        );
        );

        req.setHeader('Authorization', auth);
        req.setHeader('Content-Type', contentType);
        req.setHeader('Content-Length', Buffer.byteLength(stringPayload));

        // Add the payload
        req.write(stringPayload);

        // Bind to the error event so it doesn't get thrown
        req.on('error',function(e)
        console.log(e);
        callback(true, 'Error': e);
        );

        // Bind to the timeout event
        req.on('timeout',function()
        console.log('timeout');
        callback(true, 'Error': 'The request took much time and got timeout.')
        );

        // End the request
        req.end();
        ;


        And I call the method like this:



        genericHelper.sendRequest('https', '443', 'api.mailgun.net', 'POST', '/v3/sandbox0630029a67f24517a9c3e383d2c6098e.mailgun.org/messages',
        'application/x-www-form-urlencoded', ('Basic ' + Buffer.from(('api:'+ config.mailgun.ApiKeyTest)).toString('base64')), 5, emailRequestObject, function(err, data)

        // Here I do what I need after sending the http request with success

        );


        I hope it helps someone out, so the problem was the content-type, I had to change it to 'application/x-www-form-urlencoded' and then also the authorization I had to convert to Base64 and include Basic before the pair in base64.






        share|improve this answer












        I made it working, the current code is the following:



        helpers.sendRequest = function(protocol, port, hostname, method, path, 

        contentType, auth, timeoutSeconds, postData, callback)
        var stringPayload = querystring.stringify(postData);

        // Construct the request
        var requestDetails =
        'hostname' : hostname,
        'port': port,
        'method' : method,
        'timeout' : timeoutSeconds * 1000,
        'path' : path
        ;

        // Instantiate the request object (using either the http or https module)
        var _moduleToUse = protocol == 'http' ? http : https;
        var req = _moduleToUse.request(requestDetails, function(res)
        res.on('data', (d) =>
        if(res.statusCode == 200)
        callback(false);
        else
        console.log(res.statusCode);
        callback(true);

        );
        );

        req.setHeader('Authorization', auth);
        req.setHeader('Content-Type', contentType);
        req.setHeader('Content-Length', Buffer.byteLength(stringPayload));

        // Add the payload
        req.write(stringPayload);

        // Bind to the error event so it doesn't get thrown
        req.on('error',function(e)
        console.log(e);
        callback(true, 'Error': e);
        );

        // Bind to the timeout event
        req.on('timeout',function()
        console.log('timeout');
        callback(true, 'Error': 'The request took much time and got timeout.')
        );

        // End the request
        req.end();
        ;


        And I call the method like this:



        genericHelper.sendRequest('https', '443', 'api.mailgun.net', 'POST', '/v3/sandbox0630029a67f24517a9c3e383d2c6098e.mailgun.org/messages',
        'application/x-www-form-urlencoded', ('Basic ' + Buffer.from(('api:'+ config.mailgun.ApiKeyTest)).toString('base64')), 5, emailRequestObject, function(err, data)

        // Here I do what I need after sending the http request with success

        );


        I hope it helps someone out, so the problem was the content-type, I had to change it to 'application/x-www-form-urlencoded' and then also the authorization I had to convert to Base64 and include Basic before the pair in base64.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 19:00









        TiagoM

        1,45042560




        1,45042560



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249061%2fhttps-request-using-https-nodejs-module-of-type-form-data-multipart-not-work%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党