将post curl转换为节点请求

时间:2014-03-30 02:54:05

标签: javascript node.js curl

我想转换这个卷曲电话:

curl -d "api_token=PUT_YOUR_API_KEY_HERE&api_action=subscription_datafeed" https://example.foxycart.com/api

要使用请求lib的节点 - https://github.com/mikeal/request但我在发送正文时遇到问题。我的curl调用工作正常但是当我转换它时,我一直得到关于api_token和api_action缺失的相同错误。这是我试过的:

var options = {
  url: 'https://example.foxycart.com/api',
  body: JSON.stringify({
    api_token:'PUT_YOUR_API_KEY_HERE',
    api_action:'subscription_datafeed'
  }),
  method: 'POST'
  };

request(options, function (error, response, body) { });

此外:

var options = {
  url: 'https://example.foxycart.com/api',
  json: {
    api_token:'PUT_YOUR_API_KEY_HERE',
    api_action:'subscription_datafeed'
  },
  method: 'POST'
  };

request(options, function (error, response, body) { });

还有:

var options = {
  url: 'https://example.foxycart.com/api',
  body: '?api_token=PUT_YOUR_API_KEY_HERE&api_action=subscription_datafeed',
  method: 'POST'
  };

request(options, function (error, response, body) { 

还有:

var options = {
  url: 'https://example.foxycart.com/api',
  body: 'api_token=PUT_YOUR_API_KEY_HERE&api_action=subscription_datafeed',
  method: 'POST'
  };

request(options, function (error, response, body) { 

而且:

request.post('https://example.foxycart.com/api?api_token=PUT_YOUR_API_KEY_HERE&api_action=subscription_datafeed', function (error, response, body) { });

这是一个PHP,它可以用于同一个API上的类似调用 - 看起来我的代码是正确的但是......

$foxy_domain = "myfoxydomain.foxycart.com";
$foxyData = array();
$foxyData["api_token"] = "XXXXX your api / datafeed key here XXXXXX";
$foxyData["api_action"] = "customer_save";
$foxyData["customer_id"] = "12345";
// OR use the email:
//$foxyData["customer_email"] = "customer@example.com";
$foxyData["customer_password"] = "my new password";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://" . $foxy_domain . "/api");
curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试:

var options = {
  url: 'https://example.foxycart.com/api',
  form: {
    api_token:'PUT_YOUR_API_KEY_HERE',
    api_action:'subscription_datafeed'
  },
  method: 'POST'
};

request(options, function (error, response, body) { });