将php curl转换为Node JS请求

时间:2019-04-07 10:06:26

标签: php node.js curl request

我有以下php代码:

        //Create url
        $url = "https://pci.zcredit.co.il/WebControl/RequestToken.aspx";
        $post = "TerminalNumber=$TerminalNumber"
        ."&Username=$UserName&PaymentSum=$PaymentSum&PaymentsNumber=$PaymentsNumber&Lang=$Lang"
        ."&Currency=$Currency&UniqueID=$UniqueID&ItemDescription=$ItemDescription&ItemQtty=$ItemQtty"
        ."&ItemPicture=$ItemPicture&RedirectLink=$RedirectLink&NotifyLink=$NotifyLink"
        ."&UsePaymentsRange=$UsePaymentsRange&ShowHolderID=$ShowHolderID&AuthorizeOnly=$AuthorizeOnly"
        ."&HideCustomer=$HideCustomer&CustomerName=$CustomerName&CssType=$CssType&IsCssResponsive=$IsCssResponsive";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); // Create the request url
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post); //Set post value
        curl_setopt($ch, CURLOPT_POST, 1); // Set the request method to POST
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Not return data in brower
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $page = curl_exec($ch);    // Get the response

我想在Node js中使用Request:

let url = "https://pci.zcredit.co.il/WebControl/RequestToken.aspx";
        let post = `TerminalNumber=${TerminalNumber}`
        +`&Username=${UserName}&PaymentSum=${PaymentSum}&PaymentsNumber=${PaymentsNumber}&Lang=${Lang}`
        +`&Currency=${Currency}&UniqueID=${UniqueID}&ItemDescription=${ItemDescription}&ItemQtty=${ItemQtty}`
        +`&ItemPicture=${ItemPicture}&RedirectLink=${RedirectLink}&NotifyLink=${NotifyLink}`
        +`&UsePaymentsRange=${UsePaymentsRange}&ShowHolderID=${ShowHolderID}&AuthorizeOnly=${AuthorizeOnly}`
        +`&HideCustomer=${HideCustomer}&CustomerName=${CustomerName}&CssType=${CssType}&IsCssResponsive=${IsCssResponsive}`;

const request = require('request');

request(url +'/' + post, { json: true }, (err, res, body) => {
  if (err) { return console.log(err); }

});

但是我应该只将post参数添加到原始url吗?安全吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

使用Request发布URL编码的表单的语法很简单:

request.post(url).form({ key: value })

当然,您可以选择使用模板文字变量将请求与url中的参数一起发送,并且从安全的角度来看不会改变任何内容,但更具可读性。

如果您清理参数并使用加密(https),您的代码将是安全的,就像 main.c 在他的评论中所说的那样,您应该以任何语言进行操作。

>