将Curl请求转换为Node.JS服务器 - 将数据附加到请求中?
我正在学习如何处理信用卡支付。这里是测试卷曲...将Curl请求转换为Node.JS服务器 - 将数据附加到请求中?
curl -k -v -X POST -H "Content-Type:application/json" -H "Authorization: Basic Mxxxxxxxxxxxxxxxxxx=" -d "@json_file.txt" -o output.txt https://w1.xxxxxxxxxxxx.net/PaymentsAPI/Credit/Sale
凡json_file.txt包含
{
"InvoiceNo":"1",
"RefNo":"1",
"Memo":"TEST_TEST_PHONY",
"Purchase":"1.00",
"AccountSource":"Swiped",
"AcctNo":"5xxxxxxxxxxxxxxxxx1",
"ExpDate":"0816",
"OperatorID":"xxxxxxxxxx",
}
我转换到节点模块HTTPS
var https = require("https");
var options = {
host: 'w1.xxxxxxxxxxxxxx.net',
port: 443,
path: '/PaymentsAPI/Credit/Sale',
headers: { "Content-Type" :"application/json",
"Authorization" : "Basic Mxxxxxxxxxxxxxxxxxxxxxxxxxxx="} ,
data: {
"InvoiceNo":"1",
"RefNo":"1",
"Memo":"xxxxxxxxxxxxxxx",
"Purchase":"1.00",
"AccountSource":"Swiped",
"AcctNo":"5xxxxxxxxxxxxxxxxx1",
"ExpDate":"0816",
"OperatorID":"xxxxxxxxxxxx",
},
method: 'POST'
};
// oops... 400 Bad Request
// The request could not be understood by the server due to malformed syntax.
var req = https.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk.toString());
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
问题在原始curl请求上,JSON文本文件中包含的数据作为POST附件提交。卷曲请求正常工作。另一方面,我不清楚如何从node.js服务器执行此操作。响应头回来很好,但我得到了一个400响应(格式错误)。任何人都知道如何将JSON数据附加到HTTPS请求的附件中?
糟糕。我完全不明白req.write()的东西是如何工作的。这段代码是成功的。感谢Dan Ourada @ Mercury Payments的帮助。请注意,这里的所有代码都是纯沙盒。没有真正的美元买入。
var https = require("https");
var options = {
host: 'w1.mercurycert.net',
port: '443',
path: '/PaymentsAPI/Credit/Sale',
headers: { "Content-Type" :"application/json", "Authorization" : "Basic MDAzNTAzOTAyOTEzMTA1Onh5eg=="},
method: 'POST'
};
var inputdata = JSON.stringify({
"InvoiceNo":"1",
"RefNo":"1",
"Memo":"XXXXX",
"Purchase":"1.00",
"AccountSource":"Swiped",
"AcctNo":"549999",
"ExpDate":"0816",
"OperatorID":"money2020",
});
var req = https.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Return info: ' + chunk); // output the return raw data
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// attach input data to request body
req.write(inputdata);
req.end();
提供的信息在这里情况下别人卡从curl命令在转换为Node.js的http请求......
哎,这种玩弄后,我在惊讶将真实(安全)的支付系统纳入任何商家网站是多么容易。 (很明显需要https网站。)
另外,请查看'request'库,每个Jed Watson的回答,ref [this link](https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request -in-节点-JS) – zipzit 2017-10-22 09:06:36
为什么要投票?为什么没有评论的投票呢? – zipzit 2014-11-04 03:10:31