gpt4 book ai didi

javascript - 'form-data/multipart' 类型的 HTTPS 请求(使用 https nodejs 模块)不起作用

转载 作者:行者123 更新时间:2023-11-30 20:02:15 24 4
gpt4 key购买 nike

我有以下 curl 请求,它运行良好并且符合预期:

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!'

但是我需要使用标准 https 模块表单 nodejs 将此请求制作为有效的 https 请求,并且我尝试了以下代码,但是我一直收到 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 状态响应代码。

提前致谢!

最佳答案

我让它工作了,当前代码如下:

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 中的对之前。

关于javascript - 'form-data/multipart' 类型的 HTTPS 请求(使用 https nodejs 模块)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com