将Curl请求转换为Node

将Curl请求转换为Node

本文介绍了将Curl请求转换为Node.JS服务器 - 将数据附加到请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何处理信用卡付款。这里是测试CURL ...

I'm learning how to process credit card payments.. Here is the test CURL...

 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包含

Where json_file.txt contains

{
    "InvoiceNo":"1",
    "RefNo":"1",
    "Memo":"TEST_TEST_PHONY",
    "Purchase":"1.00",
    "AccountSource":"Swiped",
    "AcctNo":"5xxxxxxxxxxxxxxxxx1",
    "ExpDate":"0816",
    "OperatorID":"xxxxxxxxxx",
}

我转换为节点模块HTTPS

I converted over to node module 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附件。 Curl请求工作正常。另一方面,它不清楚我如何从一个node.js服务器。响应头恢复正常,但我得到400响应(格式错误的语法)。任何人都知道如何将JSON数据作为post请求附加到HTTPS请求?

Problem is on the original curl request, the data contained within the JSON text file is submitted as a POST attachment. Curl request works fine. On the other hand its not clear to me on how to do that from a node.js server. The response headers come back fine, but I get a 400 response (malformed syntax). Anybody know how to attach JSON data as a post attachment to a HTTPS request?

推荐答案

我完全不明白如何req.write()东西工作..这段代码是成功的。感谢Dan Ourada @ Mercury Payments的帮助。注意,这里的所有代码都是纯沙箱。没有真正的$$去买。

Oops. I totally didn't understand how the req.write() stuff works.. This code is successful. Thanks to Dan Ourada @ Mercury Payments for his assistance. Note, all code here is pure sandbox. No real $$ going buy buy.

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":"5499990123456781",
    "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请求...

Info offered here in case anybody else gets stuck converting from a CURL command over to a Node.js http request...

然而,在玩过这个之后,我惊讶于将真实(和安全)支付系统融入任何商家网站。 (显然需要Https站点。)

And hey, after playing around with this, I'm amazed on how truly easy it is to incorporate a real (and secure) payments system into any merchant website. (Obviously Https site required.)

这篇关于将Curl请求转换为Node.JS服务器 - 将数据附加到请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:55