在Node.js中模拟curl请求

时间:2018-09-05 18:12:56

标签: javascript node.js http curl pfx

我必须在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',
    }
  }
)

获取“ 错误:标记错误”。但是它可以卷曲。 我将不胜感激。

2 个答案:

答案 0 :(得分:0)

以下工作吗?

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);

答案 1 :(得分:0)

所以,这是解决方法:

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

仍然想知道如何使用“ pfx”选项...