【问题标题】:Node JS - Posting HTML form (or FormData) from my Node server to a 3rd party serverNode JS - 从我的 Node 服务器发布 HTML 表单(或 FormData)到第 3 方服务器
【发布时间】:2021-12-29 15:39:26
【问题描述】:

问题:

我有一个 HTML 表单,我想发送到第 3 方服务器(信用卡处理器)。当我从我的 React 客户端呈现表单时,它会被提交并且我会收到我应该收到的响应。

问题是它包含很多敏感信息,例如我想对客户隐藏的商户 ID 和 Sha1Hash。所以我希望能够从我的节点服务器上执行表单,而不是在客户端上执行。

我想做的事:

  1. 将一些不那么敏感的客户端信息发布到我的 Node 服务器(我知道该怎么做),
  2. 然后让 Node 服务器填写包含敏感信息的新表单并将其发布到第 3 方服务器(我不知道该怎么做)
  3. 从服务器接收响应(响应是一个托管支付页面)并显示在客户端(我不知道我是否知道如何做到这一点)

我不知道如何在 Node 端使用 HTML 表单,所以我尝试使用 FormData 并通过 axios 发送它,但是当我没有设置标题时它给了我 ECONRESET 错误。我不知道要设置什么作为标头,我基本上是从 Chrome Dev 工具中复制并粘贴了 Request Header 部分。它停止给我 ECONRESET 错误,但它没有正确读取数据。

let formData = generateFormData(req.body, hash)
let header = {
    "Connection": "keep-alive",
    "Content-Length": "907",
    "Cache-Control": "max-age=0",
    "Content-Type": "application/x-www-form-urlencoded",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Dest": "document",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-CA,en;q=0.9,ko-KR;q=0.8,ko;q=0.7,en-GB;q=0.6,en-US;q=0.5,fr;q=0.4"
}
console.log('formdata', formData)

axios({
    method: "post",
    url: actionUrl,
    data:formData,
    headers: header
})
.then(function (response) {
    //handle success
    console.log('succeeeeeeeeeeeeeeeeeeeeeeeed')
    console.log('response', response)
    return res.send(response)
  })
  .catch(function (response) {
    //handle error
    console.log('faaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaail')
    console.log(response)
    return res.send(response)
  });

【问题讨论】:

    标签: html node.js reactjs forms


    【解决方案1】:

    我遇到了同样的问题这是我的解决方案, 确保添加与 html 页面中相同的所需标题

    const FormData = require('form-data');
    const formData = new FormData();
    formData.append('firstname', "yourname");
    const requestConfig = {
        method: 'POST',
        url: actionUrl,
        headers: {
            ...formData.getHeaders(),
            'Content-Type': 'application/x-www-form-urlencoded'
    //add required headers 
        },
        data: formData
    };
    Axios(requestConfig)
        .then((response) => {
            if (response) {
                console.log("RESPONSE:", response);
            }
        })
        .catch((err) => {
            if (err) {
                console.error("ERR:", err);
            }
        })
    

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 2015-09-09
      • 2020-06-30
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 2017-10-08
      相关资源
      最近更新 更多