如何使用node.js库表单数据做一个https post?

我们有一个客户端需要通过https的post。 我们有一个node.js应用程序,并且已经使用felixge的node-form-data包( https://github.com/felixge/node-form-data )来完成提交。 虽然这些调用对客户端的testing基于http的api工作正常,但是当我们尝试调用使用https的生产api时,api响应客户端告诉我们的400响应是由于通过http进入的。

有关我们如何在我们的提交中指定https的任何想法?

我们的代码如下所示:

var FormData = require('form-data'); var couponForm = new FormData(); couponForm.append('data','{"coupon":{"code":"' + couponCode + '", "discount": "' + discountPercentage + '", "type": "percent", "product": "' + productId + '", "times": "1", "expires": "' + couponExpiresDt + '"}}'); couponForm.submit({hostname:config.client_api_host, path:'/api/coupon/add', auth:auth}, function(err, res) { res.resume(); if (err) { logger.log('error', 'Client API createDiscount post error:'); logger.log('error', {err: err}); callback(err); } else if (res.statusCode != 200) { logger.log('error', 'Client API createDiscount post response error:'); console.log('error', res); logger.log('error', {statusCode: res.statusCode}); logger.log('error', {body: res.body}); callback(new Error('Client API createDiscount post response error:', res.statusCode)); } else { logger.log('info', "Client coupon code " + couponCode + " has apparently been created"); callback(null, {coupon_code: couponCode, expires: couponExpiresDt}); } }); 

根据这个你需要在你的选项中传递protocol: 'https:'

 couponForm.submit({ hostname: config.client_api_host, path: '/api/coupon/add', auth: auth, protocol: 'https:' }, function(err, res) { [...] });