【问题标题】:Emulate curl request in nodejs在 nodejs 中模拟 curl 请求
【发布时间】:2018-09-17 20:53:17
【问题描述】:

我必须在 nodejs 中模拟 curl 请求:

curl -k POST https://example.com/ --cert mycert.pem:password

我已经写了一些代码,但它的工作方式不相似:

request.post(
  {
    'url': 'https://example.com/',
    'agentOptions': {
      'pfx': fs.readFileSync('./mycert.pem'),
      'passphrase': 'password',
    }
  }
)

得到“错误:错误标签”。但它适用于卷曲。 如有任何帮助,我将不胜感激。

【问题讨论】:

  • 很确定 fs 不是 pem 解析器。
  • 为什么不呢? PEM - 最后只是带有证书列表和私钥列表的文本文件。

标签: javascript node.js http curl pfx


【解决方案1】:

以下是否有效?

const exec = require('child-process-promise').exec;
const cmd = 'curl -k POST https://example.com/ --cert mycert.pem:password';
exec(cmd).then(res => console.log(res.stdout)).catch(console.error);

【讨论】:

  • 是的,它有效。但不是正确的解决方案。应该通过nodejs工具来完成,以避免操作系统不兼容。
【解决方案2】:

所以,这里是解决方案:

request.post(
  {
    'url': 'https://example.com/',
    'key': {
      'cert': fs.readFileSync('./mycert.pem'),
      'key': fs.readFileSync('./mycert.pem'),
      'passphrase': 'password',
    }
  }
)

还在疑惑,如何使用“pfx”选项...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2021-11-09
    • 2017-07-19
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    相关资源
    最近更新 更多