node.js - AWS Lambda 上的 curl 请求的 NodeJS 12.x https 版本
问题描述
curl -d "m_payment_id=m_payment_id-xxxxxxxxxxxxxxxxxyyy&pf_payment_id=990396&payment_status=COMPLETE&item_name=Subscription&item_description=Monthly+Subscription&amount_gross=99.00&amount_fee=-6.74&amount_net=92.26&custom_str1=&custom_str2=&custom_str3=&custom_str4=&custom_str5=&custom_int1=&custom_int2=&custom_int3=&custom_int4=&custom_int5=&name_first=&name_last=&email_address=christo%40g4-ape.co.za&merchant_id=0000000&token=0000000-0000-0000-3a83-25bc733a307b&billing_date=2020-02-21&signature=3895d0769b56862b842da5067af4483f" -X POST https://sandbox.somedomain.co.za/what/something/validate
我的尝试:
const https = require("https");
const querystring = "m_payment_id=m_payment_id-xxxxxxxxxxxxxxxxxyyy&pf_payment_id=990396&payment_status=COMPLETE&item_name=Subscription&item_description=Monthly+Subscription&amount_gross=99.00&amount_fee=-6.74&amount_net=92.26&custom_str1=&custom_str2=&custom_str3=&custom_str4=&custom_str5=&custom_int1=&custom_int2=&custom_int3=&custom_int4=&custom_int5=&name_first=&name_last=&email_address=christo%40g4-ape.co.za&merchant_id=0000000&token=0000000-0000-0000-3a83-25bc733a307b&billing_date=2020-02-21&signature=3895d0769b56862b842da5067af4483f";
return new Promise((resolve, reject) => {
const options = {
hostname: 'sandbox.somedomain.co.za',
port: 443,
path: 'what/something/validate',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(querystring)
}
};
const req = https.request(options, (res) => {
console.log('statusCode: ' + res.statusCode);
console.log('headers: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('BODY: ' + data);
});
resolve('Success');
});
req.on('error', (e) => {
console.log('problem with request: ' + e.message);
reject(e.message);
});
// write data to request body
req.write(querystring);
req.end();
});
我在 NodeJS 代码上不断收到 statusCode 400,curl 工作正常。为了安全起见,主机名 h 显然已更改。
有人可以告诉我我在这里做错了什么吗?
解决方案
当您在发布时发送正文时需要 Content-Length。在您的情况下,所有信息都作为查询字符串参数传递,因此您可以摆脱所有标题。
另外,你应该在里面解决res.on('end'
。你这样做的方式将在函数完成执行之前完成它。
推荐阅读
- python - Python .NET WinForms - 如何将信息从文本框传递到按钮单击事件
- c# - Oracle 托管数据访问 - ORA-01017
- c# - Xamarin.Forms.WeakEventManager .ctor() 无法从 Xamarin.CommunityToolkit 访问
- php - 默认关闭 WooCommerce 产品选项卡 - Greenmart 主题
- r - R数据表冻结前N列
- docker - 如何将 docker 清单文件保存到 tar 文件
- vue.js - 如何在设置/渲染函数中将侦听器添加到事件“更新:myprop”
- python - 使用 Python 获取无效的 BSON MongoDb $project
- android - Jetpack compose DropdownMenu 带有圆角
- javascript - Fetch API - http 和 https 的 CORS 问题