HTTPS-запрос (с использованием модуля https nodejs) типа 'form-data / multipart' не работает - PullRequest
0 голосов
/ 11 ноября 2018

У меня есть следующий запрос curl, который работает хорошо и как положено:

curl --user 'api: MY_API_KEY' https://api.mailgun.net/v3/mydomain/messages --form from = 'Мое имя '--form to=validaddress@domain.com --form subject = 'Привет 3!' --form text = 'Тестирование отправки электронной почты!'

Однако мне нужно обработать этот запрос как действительный запрос https, используя стандартную форму модуля https nodejs, и я попробовал следующий код, однако я продолжаю получать 400 (неправильный запрос) в качестве ответа:

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.boundary}\n`;

   // 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();
 };

Может кто-нибудь дать мне несколько советов, указаний или советов? Я немного ошеломлен этим, я уверен, что это может быть что-то простое, я делал проб и ошибок с точками с запятой и тире на границе, но все еще не получил код ответа 200.

Заранее большое спасибо!

1 Ответ

0 голосов
/ 11 ноября 2018

Я сделал это работает, текущий код выглядит следующим образом:

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();
 };

И я называю метод следующим образом:

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

});

Я надеюсь, что это кому-то поможет, поэтому проблема заключалась в типе контента, мне пришлось изменить его на «application / x-www-form-urlencoded», а затем авторизацию, которую мне пришлось преобразовать в Base64 и включить Basic перед парой в base64.

...